This is what the Exercise says: Write a class called fueler that can keep track
ID: 3620021 • Letter: T
Question
This is what the Exercise says:Write a class called fueler that can keep track of the fuel and mileage of a vehicle. Include private member variables to track the amount of fuel that the vehicle has consumed and the distance that the vehicle has traveled. You are to measure the fuel units in U.S. gallons and document that it is in gallons at the point where you declare the variables.
The class should have a constructor that initializes these variables to zero. Include a member function that can later reset both variables to zero. There are two different modification member functions to add a given amount to the total distance driven (one has a miles parameter, and the other has a kilometers parameter); similarly, there are three member functions to a given amount to the total fuel consumed (with different units for the amount of fuel).
The class has two const member function to retrieve the total distance driven (in miles or km), three functions for the fuel consumed (in U.S. gallons) and four for the fuel milage (in U.S. mpg).
Explanation / Answer
please rate - thanks #include <iostream>using namespace std;
class fueler{
private:
double fuel;
double mileage;
public:
fueler()
{fuel=0;
mileage=0;
}
void reset()
{fuel=0;
mileage=0;
}
void addmiles(double miles)
{mileage+=miles;
}
void addkilo(double kilo)
{mileage+=kilo/1.61;
}
double getmiles()
{return mileage;
}
double getkilo()
{return mileage*1.61;
}
double getmpg()
{return mileage/fuel;
}
double getkpl()
{return (mileage*1.61)/(fuel*3.7854);
}
void addliters(double liters)
{fuel+=(liters/3.7854);
}
void addgallons(double gal)
{fuel+=gal;
}
double getgallons()
{return fuel;
}
double getliters()
{return fuel*3.7854;
}
};
int main()
{fueler a;
a.addmiles( 20);
a.addkilo(50);
a.addgallons(5);
a.addliters(3);
cout<<"gallons used: "<<a.getgallons()<<endl;
cout<<"liters used: "<<a.getliters()<<endl;
cout<<"miles driven: "<<a.getmiles()<<endl;
cout<<"kilometers driven: "<<a.getkilo()<<endl;
cout<<"miles per gallon used: "<<a.getmpg()<<endl;
cout<<"kilos per liter used: "<<a.getkpl()<<endl;
a.reset();
cout<<"liters used: "<<a.getliters()<<endl;
cout<<"miles driven: "<<a.getmiles()<<endl;
system("pause");
return 0;
}
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.