Implement a class Car with the following properties. A car has a certain fuel ef
ID: 3636186 • 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
please rate - thanks
#include <string>
#include <iostream>
using namespace std;
class Car
{
public:
Car(double);
void add_gas(double);
void drive(double);
double get_gas();
private:
double mileage;
double gallons;
};
Car::Car(double m)
{ mileage=m;
gallons=0;
}
void Car::add_gas(double g)
{gallons+=g;
}
void Car::drive(double m)
{gallons-=(m/mileage);
}
double Car::get_gas()
{
return gallons;
}
int main()
{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
system("pause");
return 0;
}
Related Questions
drjack9650@gmail.com
Navigate
Integrity-first tutoring: explanations and feedback only — we do not complete graded work. Learn more.