Write a class car with the following properties: A car has a certain fuel effici
ID: 3621950 • Letter: W
Question
Write a class car with the following properties: A car has a certain fuel efficiency measured in miles/gallon and a certain amount of fuel in the tank. The efficiency is specified in the constructor and the initial fuel level of a car is 0.0. Provide a member function drive(double miles) that simulates driving the car for a certain distance, reducing the fuel level in the tank, and member functions get_fuel(), returning the current fuel level, and add_fuel(double gallons) to fuel up.#include <iostream>
using namespace std;
class car
{
private:
double fuel_efficiency; // mileage in miles/gallon
double fuel_capacity; // capacity of fuel tank in gallons
double fuel_in_tank; // current fuel in tank in gallons
public:
car(double efficiency, double capacity); // all cars start
// with empty tanks
double get_fuel() const; // returns current fuel level
void add_fuel(double gallons); // increases fuel in the tank
// either by the gallons specified
// or the amount which fills the
// tank, whichever is smaller.
void drive(double miles); // reduces fuel in the tank
};
// ***** Insert implementations of the member functions here. *****
int main() {
car c1(15.0, 20.0), c2(29.5, 30.0);
c2.add_fuel(10.0);
c2.drive(82.5);
c1.add_fuel(25.0);
c2.add_fuel(20.0);
c2.drive(65.5);
c1.drive(55.7);
c2.add_fuel(8.5);
c2.drive(18.4);
cout << "c1 fuel remaining: " << c1.get_fuel() << endl;
cout << "c2 fuel remaining: " << c2.get_fuel() << endl;
}
Explanation / Answer
please rate - thanks
#include <iostream>
using namespace std;
class car
{
private:
double fuel_efficiency; // mileage in miles/gallon
double fuel_capacity; // capacity of fuel tank in gallons
double fuel_in_tank; // current fuel in tank in gallons
public:
car(double efficiency, double capacity); // all cars start
// with empty tanks
double get_fuel() const; // returns current fuel level
void add_fuel(double gallons); // increases fuel in the tank
// either by the gallons specified
// or the amount which fills the
// tank, whichever is smaller.
void drive(double miles); // reduces fuel in the tank
};
// ***** Insert implementations of the member functions here. *****
int main() {
car c1(15.0, 20.0), c2(29.5, 30.0);
c2.add_fuel(10.0);
c2.drive(82.5);
c1.add_fuel(25.0);
c2.add_fuel(20.0);
c2.drive(65.5);
c1.drive(55.7);
c2.add_fuel(8.5);
c2.drive(18.4);
cout << "c1 fuel remaining: " << c1.get_fuel() << endl;
cout << "c2 fuel remaining: " << c2.get_fuel() << endl;
system("pause");
}
car::car(double efficiency, double capacity) // all cars start
// with empty tanks
{fuel_in_tank=0;
fuel_efficiency=efficiency;
fuel_capacity=capacity;
}
double car:: get_fuel() const // returns current fuel level
{return fuel_in_tank;
}
void car::add_fuel(double gallons) // increases fuel in the tank
// either by the gallons specified
// or the amount which fills the
// tank, whichever is smaller.
{fuel_in_tank+=gallons;
if(fuel_in_tank>fuel_capacity)
fuel_in_tank=fuel_capacity;
}
void car:: drive(double miles) // reduces fuel in the tank
{
fuel_in_tank= fuel_in_tank-miles/fuel_efficiency;
}
Related Questions
drjack9650@gmail.com
Navigate
Integrity-first tutoring: explanations and feedback only — we do not complete graded work. Learn more.