Define a class called Odometer that will be used to track fuel and mileage
for an automotive vehicle. Include private member variables to track the
miles driven and the fuel efficiency of the vehicle in miles per gallon. The
class should have a constructor that initializes these values to zero.
Include a member function to reset the odometer to zero miles, a member function to set the fuel efficiency, a member function that accepts miles driven for a trip and adds it to the odometer’s total, and a member 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.
#include <iostream>
using namespace std;
/*----------class Odometer-------------*/
class Odometer{
double milesDriven, fuelEfficent;
public:
Odometer(); //default constructor that setsmiles and fuel to 0
void resetMiles(); //member function to resetmiles
void setFuelEfficent(double amount); // memberfunction to set fuel efficent
void addMiles(double miles); //member functionadding miles
double getConsumption(); //member function toreturn # of gallons of gas since last reset
double getMiles(); //member function gettingmiles
double getFuelEfficent(); //member functiongetting fuelefficent
};
Odometer::Odometer(){
milesDriven = 0;
fuelEfficent = 0;
};
void Odometer::resetMiles(){
milesDriven = 0;
}
void Odometer::setFuelEfficent(double amount){
fuelEfficent = amount;
}
void Odometer::addMiles(double miles){
milesDriven += miles;
}
double Odometer::getConsumption(){
return milesDriven / 10; //made up value of 10miles per gallon
}
double Odometer::getMiles(){
return milesDriven;
}
double Odometer::getFuelEfficent(){
return fuelEfficent;
}
int main(){
Odometer test1; //constructor that sets all values to 0
cout << "Initial Odometer: " << endl;
cout << "Miles Driven: " << test1.getMiles()<< endl;
cout << "Fuel Efficency: " <<test1.getFuelEfficent() << endl << endl;
cout << "Adding 54.5 miles to odometer" <<endl;
test1.addMiles(54.5); //add 54.5 miles
cout << "Miles Driven: " << test1.getMiles()<< endl << endl;
cout << "The consumption rate of 54.5 is: " <<endl;
//find out the consumption rate
cout << "Fuel Efficency: " <<test1.getConsumption() << " miles per gallon(MPG)" <<endl << endl;
cout << "Reseting the odometer" << endl;
test1.resetMiles(); //reset miles
cout << "Miles Driven: " << test1.getMiles()<< endl;
return 0;
}