Question
C++, Visual Studio
3. Define a class called Odometer that will be used to track fuel and mileage for an automobile. The class should have instance variables to track the miles driven and the 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 odometers total, and an accessor method hat 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 variables should be public, if any.
0 0
Add a comment Improve this question Transcribed image text
Answer #1

Program:

#include <iostream>

using namespace std;

// Creating Odometer class
class Odometer
{
private:
// Declaring variables
int milesDriven;
double fuelEfficiency;
int noOfGallonsUsed;

public:
int totalMilesDriven;

Odometer();
// Function declarations
void reset();
void setFuelEfficiency(double FuelEff);
void addMiles(int milesDrivenInTrip);
double getGasolineConsumed();
};
// Default constructor
Odometer::Odometer()
{
totalMilesDriven = 0;
fuelEfficiency = 0.0;
}

// Function implementations
void Odometer::reset()
{
this->milesDriven = 0;
}
void Odometer::setFuelEfficiency(double FuelEff)
{
this->fuelEfficiency = FuelEff;
}
void Odometer::addMiles(int milesDrivenInTrip)
{
milesDriven = milesDrivenInTrip;
totalMilesDriven += milesDrivenInTrip;
}
double Odometer::getGasolineConsumed()
{
return milesDriven / fuelEfficiency;
}


int main()
{
// Declaring variables
int noOfMilesDriven;
double fuelEfficiency;

// Create a instance of Odometer class
Odometer trip1;

// getting the data entered by the user
cout << "Enter the no of Miles Driven in trip#1:";
cin >> noOfMilesDriven;

// Calling the setter method
trip1.addMiles(noOfMilesDriven);

// getting the fuel efficiency entered by the user
cout << "Enter Fuel Efficiency :";
cin >> fuelEfficiency;

// Calling the setter method
trip1.setFuelEfficiency(fuelEfficiency);

// Displaying the no of gallons used in that trip#1
cout << "No of Gallons of Gasoline used for " << noOfMilesDriven
<< " in trip#1 :" << trip1.getGasolineConsumed() << endl;

trip1.reset();

Odometer trip2;
cout << "Enter the no of Miles Driven in trip#2:";
cin >> noOfMilesDriven;

trip2.addMiles(noOfMilesDriven);

cout << "Enter Fuel Efficiency :";
cin >> fuelEfficiency;
trip2.setFuelEfficiency(fuelEfficiency);

// Displaying the no of gallons used in that trip#2
cout << "No of Gallons of Gasoline used for " << noOfMilesDriven
<< " in trip#1 :" << trip2.getGasolineConsumed() << endl;

// Displaying the total no of gallons used
cout << "Total No of Miles driven :" << trip2.totalMilesDriven << endl;
}

Output:

Enter the no of Miles Driven in trip#1:450 Enter Fuel Efficiency 23 No of Gallons of Gasoline used for 450 in trip#1 :19-5652 Enter the no of Miles Driven in trip#2 : 356 Enter Fuel Efficiency 15 No of Gallons of Gasoline used for 356 in trip#1 :23.7333 Total No of Miles driven :356 . Process exited after 15.29 seconds with return value 0 Press any key to continue .

Add a comment
Know the answer?
Add Answer to:
C++, Visual Studio 3. Define a class called Odometer that will be used to track fuel...
Your Answer:

Post as a guest

Your Name:

What's your source?

Earn Coins

Coins can be redeemed for fabulous gifts.

Not the answer you're looking for? Ask your own homework help question. Our experts will answer your question WITHIN MINUTES for Free.
Similar Homework Help Questions
ADVERTISEMENT
Free Homework Help App
Download From Google Play
Scan Your Homework
to Get Instant Free Answers
Need Online Homework Help?
Ask a Question
Get Answers For Free
Most questions answered within 3 hours.
ADVERTISEMENT
ADVERTISEMENT
ADVERTISEMENT