C++ Define a class called Odometer that will track fuel and mileage for an automotive vehicle.

1
C++ Define a class called Odometer that will track fuel and mileage for an automotive vehicle.

LANGUAGE: C++

CHALLENGE:

C++ Define a class called Odometer that will track fuel and mileage for an automotive vehicle. The class should have member variables to track the miles driven and the fuel inefficiency of the vehicle in miles per gallon. Include a mutator function to reset the odometer to zero miles, a mutator function to set the fuel efficiency, a mutator function that accepts miles driven for a trip and adds it to the odometer’s total, and an accessor function that returns the number of gallons of gasoline that the vehicle has consumed since the odometer was last reset.

Use your class with a test program that creates several trips with different fuel efficiencies. You should decide which variable should be public, if any.


SOLUTION:

#include<iostream>
#include <iomanip> //included for format cout to two decimals

using namespace std;

class Odometer{
    private:
        double odometerTotal;
        double gallonsUsedTotal;
    public:
        Odometer();
        void resetOdometer();
        void Odometer::addMilesDriven(double (milesDriven));
        void Odometer::addGallonsUsed(double (fnGallonsUsed));
        double Odometer::getTripMpg(double (milesDriven),double (fnGallonsUsed));
        double Odometer::getTotalMiles();
        double Odometer::getTotalGallons();
        double Odometer::getTotalMpg();
};

Odometer::Odometer(){
    odometerTotal = 0;
    gallonsUsedTotal = 0;
}

void Odometer::resetOdometer(){
    odometerTotal = 0;
    gallonsUsedTotal = 0;
}

void Odometer::addMilesDriven(double (milesDriven)){
    odometerTotal = odometerTotal + milesDriven;
}

void Odometer::addGallonsUsed(double (fnGallonsUsed)){
    gallonsUsedTotal = gallonsUsedTotal + fnGallonsUsed;
}

double Odometer::getTripMpg(double (milesDriven),double (fnGallonsUsed)){
    return milesDriven / fnGallonsUsed;
}

double Odometer::getTotalMiles(){
    return odometerTotal;
}

double Odometer::getTotalGallons(){
    return gallonsUsedTotal;
}

double Odometer::getTotalMpg(){
    if(odometerTotal == 0 || gallonsUsedTotal == 0){
        return 0;
    }else{
        return odometerTotal / gallonsUsedTotal;
    }
}

int main(){
    Odometer odom;
    double milesDriven,gallonsUsed;
    int option;

    std::cout << std::setprecision(2) << std::fixed;

    do {
        cout << "Select an Option: \n (1) Enter Trip \n (2) View Totals \n (3) Reset Odometer \n (4) Exit" << endl; cin >> option;

        switch (option){
            case 1:
                cout << "Enter the number of miles driven: "; cin >> milesDriven;
                odom.addMilesDriven(milesDriven);

                cout << "Enter Gallons Used: "; cin >> gallonsUsed;
                odom.addGallonsUsed(gallonsUsed);

                cout << "This Trip Averaged " << odom.getTripMpg(milesDriven,gallonsUsed) << " MPG. \n" << endl;
                break;
            case 2:
                cout << "Number of Miles Driven Since Last Refuel: " << odom.getTotalMiles() <<      endl;
                cout << "Number of Gallons Used Since Last Refuel: " << odom.getTotalGallons() << endl;
                cout << "OverAll MPG: " << odom.getTotalMpg() << " \n" << endl;
                break;
            case 3:
                odom.resetOdometer();
                break;
            case 4:
                break;
        }
    }while(option != 4);

    system("pause");
    return 0;
}

One comment

Add comment