Answer to Question #216277 in C++ for kaydee

Question #216277
Define a class called Odometer that can be used to track fuel and traveling distance for a car. Include private member variables to track the distance driven (in kms) and the fuel efficiency of the car in kilometers per liter. The class should have a constructor that initializes these values to zero. Include  a default constructor to initialize both member variables to zero,  a member function to reset the odometer to zero kilometers,  a member function to set the fuel efficiency,  a member function that accepts kilometers driven for a trip and adds it to the odometer’s total,  a member function that returns the number of liters of fuel that the car used since the odometer was last reset (this can be calculated using the distance driven and the fuel efficiency), and  a destructor. Use your class with a test program that creates odometers for different cars with different fuel efficiencies. Let the cars do several trips. Reset the odometers at the start of your program.
1
Expert's answer
2021-07-12T17:18:34-0400
#include <iostream>
using namespace std;


class Odometer{
    double mD;
    double fuelEf;


public:
    Odometer(){
        mD = 0;
        fuelEf = 0;
    }
    void resetMiles(){
        mD = 0;
    }
    void setFuelEfficent(double amount){
        fuelEf = amount;
    }
    void addMiles(double miles){
        mD += miles;
    }
    double getConsumption(){
        return mD / 10;
    }
    double getMiles(){
        return mD;
    }
    double getFuelEfficent(){
        return fuelEf;
    }
};




int main(){


    Odometer t1;
    
    cout << "Initial Odometer: " << endl;
    cout << "Miles Driven: " << t1.getMiles()<< endl;
    cout << "Fuel Efficency: " <<t1.getFuelEfficent()<< endl;
    
    cout << "Adding 54.5 miles to odometer" <<endl;
    t1.addMiles(54.5);
    cout << "Miles Driven: " << t1.getMiles()<< endl;
    cout << "The consumption rate of 54.5 is: " <<endl;
    cout << "Fuel Efficency: " <<t1.getConsumption() << " miles per gallon(MPG)" <<endl;
    cout << "Reseting the odometer" << endl;
    t1.resetMiles();
    cout << "Miles Driven: " << t1.getMiles()<< endl;
    
    return 0;
}

Need a fast expert's response?

Submit order

and get a quick answer at the best price

for any assignment or question with DETAILED EXPLANATIONS!

Comments

No comments. Be the first!

Leave a comment

LATEST TUTORIALS
New on Blog
APPROVED BY CLIENTS