Define a class called odometer that will be used to track fuel and mileage for a
ID: 3841976 • Letter: D
Question
Define a class called odometer that will be used to track fuel and mileage for an the automobile, The class should have instance variables to track the miles driven and fuel efficiency of the vehicle in miles per gallon. Include a mutator method to reset the odometer to zero miles, a mutator method to set the fuel efficiency, a mutator method that accepts miles driven for a trip and adds it to the odometer's total, and an accessor method hat returns the number of gallons of gasoline that the vehicle has consumed since the odometer was last reset.Explanation / Answer
class Odometer
{
private:
double miles_driven;
double efficiency;
public:
Odometer()
{
miles_driven = 0;
efficiency = 0;
}
void reset()
{
miles_driven = 0;
}
void set_efficiency(double eff)
{
efficiency = eff;
}
void add_miles(double miles)
{
miles_driven += miles;
}
double fuel_consumed()
{
return miles_driven / efficiency;
}
};
Related Questions
Hire Me For All Your Tutoring Needs
Integrity-first tutoring: clear explanations, guidance, and feedback.
Drop an Email at
drjack9650@gmail.com
drjack9650@gmail.com
Navigate
Integrity-first tutoring: explanations and feedback only — we do not complete graded work. Learn more.