Define a class called Odometer that will be used to track fuel and mileage tor a
ID: 3843703 • Letter: D
Question
Define a class called Odometer that will be used to track fuel and mileage tor 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 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 save the odometer was last reset.Explanation / Answer
#include <iostream>
#include<conio.h>
using namespace std;
class odometer{
public:
float miles,fuel,mileage;
void reset()//Resets the fuel to 0
{
miles=0;
}
void efficiency(float a)//Takes a value as an argument and puts it in mileage
{
mileage=a;
}
void add(float h)//Adds miles to the miles counter
{
miles=miles+h;
}
float fuelused()//Finds the fuel efficiency
{
if(miles==0) {cout<<" The odometer has just been reset"; return 0;}
else{
fuel=miles/mileage;
return fuel;
}
}
}car1;
void main()
{
int a;
float eff,m;
while(1)
{
cout<<"Enter choice. 1. For resetting the odometer. 2. For setting fuel efficiency. 3. For adding miles traveled. 4. For getting fuel used ";
cin>>a;
switch(a)
{
case 1:
{
cout<<"Are you sure you want to reset odometer(y or n) ";
if((getch())=='y'|'Y') car1.reset();
else break;
break;
}
case 2:
{
cout<<"Enter fuel efficiency in miles/gallon";
cin>>eff;
car1.efficiency(eff);
break;
}
case 3:
{
cout<<"Enter number of miles traveled";
cin>>m;
car1.add(m);
break;
}
case 4:
{
cout<<" Fuel used of car is"<<car1.fuelused()<<" gallons";
break;
}
}
fflush(stdin);
cout<<"Do you want to start again?(y or n)";
if((getch())=='n') break;
}
getch();
}
Related Questions
drjack9650@gmail.com
Navigate
Integrity-first tutoring: explanations and feedback only — we do not complete graded work. Learn more.