Define a class called Odometer that will be used to track fuel and mileage for a
ID: 3844204 • Letter: D
Question
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 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
#include <iostream>
#include <stdio.h>
#include <math.h>
using namespace std;
class Odometer
{
private:
int miles;
double mpg, gas;
public:
Odometer()
{
miles = 0;
mpg = 0;
gas = (miles/mpg);
}
Odometer(int m, double mpg1, double ga)
{
miles = m;
mpg1 = mpg;
}
void sMiles(int m)
{
miles = miles + m;
}
int gMiles()
{
return miles;
}
void sMpg(double mpg1)
{
mpg = mpg1;
}
void eff(double ga)
{
gas = ga;
ga = (miles/mpg);
}
double gGas()
{
return gas;
}
void display()
{
cout<<" Dirven:" <<miles;
cout<<" Miles:"<<mpg;
}
};
int main()
{
Odometer trip1;
Odometer trip2;
trip1.sMpg(45);
trip1.sMiles(115);
std::cout<<" For Trip1:";
trip1.display();
trip1.sMiles(60);
std::cout<<" After 60 miles";
trip1.display();
trip2.sMpg(30);
trip2.sMiles(135);
std::cout<<" For Trip2:";
trip2.display();
trip2.sMiles(60);
std::cout<<" After 60 miles";
trip2.display();
return 0;
}
OUTPUT
For Trip1:
Dirven:115
Miles:45
After 60 miles
Dirven:175
Miles:45
For Trip2:
Dirven:135
Miles:30
After 60 miles
Dirven:195
Miles:30
Related Questions
drjack9650@gmail.com
Navigate
Integrity-first tutoring: explanations and feedback only — we do not complete graded work. Learn more.