Implement a class Car with the following properties. A car has a certain fuel ef
ID: 3639820 • Letter: I
Question
Implement a class Car with the following properties. A car has a certain fuel efficiency (measured in miles/gallon or liters/km—pick one) and a certain amount of fuel in the gas tank. The efficiency is specified in the constructor, and the initial fuel level is 0. Supply a function drive that simulates driving the car for a certain distance, reducing the fuel level in the gas tank, and functions get_gas, to return the current fuel level, and add_gas, to tank up. Sample usage:Car my_beemer(29); // 29 miles per gallon
my_beemer.add_gas(20); // Tank 20 gallons
my_beemer.drive(100); // Drive 100 miles
cout << my_beemer.get_gas() << " "; // Print fuel remaining
Explanation / Answer
class Car
{
int fuel;
public:
void Car()
{
fuel=0;
}
void Car(int a)
{
efficiency=a;
}
void add_gas()
{
int a;
cout<<"Enter gas (in gallons) to be filled.. ";
cin>>a;
fuel+=a;
}
void drive()
{
int dist;
cout<<"Enter distance (in miles) driven.. ";
cin>>dist;
fuel = fuel - (distance/efficiency);
}
void get_gas()
{
cout<<"Available fuel in tank = "<<fuel<<" gallons";
}
};
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.