Answer to Question #218890 in C++ for Nikita

Question #218890

An odometer is an instrument used for measuring the distance travelled by a vehicle. 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 and the fuel efficiency of the car in kilometres per litre. The class should have a constructor that initializes these values to 0. Include a default constructor to initialize both member variables to zero, a member function to reset the odometer to zero km, a member function to set the fuel efficiency, a member function that accepts km driven for a trip and adds it to the odometer’s total, a member function that returns the number of L of fuel that the car used since the odometer was last reset, 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. Display the distance and the litres of fuel consumed after each trip. 


1
Expert's answer
2021-07-20T02:32:48-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 Efficiency: " <<t1.getFuelEfficent()<< endl;

    cout << "Adding 54.5 miles to odometer" <<endl;
    t1.addMiles(54.5);
    cout << '\n';
    cout << "Miles Driven: " << t1.getMiles()<< endl;
    cout << "The consumption rate of 54.5 is: " <<endl;
    cout << "Fuel Efficiency: " <<t1.getConsumption() << " miles per gallon(MPG)" <<endl;
    cout << "Resting the odometer" << endl;
    t1.resetMiles();
    cout << '\n';
    cout << "Miles Driven: " << t1.getMiles()<< endl;

    return 0;
}

Output:
Initial Odometer: 
Miles Driven: 0
Fuel Efficiency: 0
Adding 54.5 miles to odometer


Miles Driven: 54.5
The consumption rate of 54.5 is: 
Fuel Efficiency: 5.45 miles per gallon(MPG)
Resting the odometer


Miles Driven: 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