Practice

Hybrid

What is the exact output of the following program?  Show your intermediate work.

 ``` #include using namespace std; #define CUTOFF 30 #define C_RATE 1 #define U_RATE 2 #define F_RATE 0.1 class Car { int odometer; double fuel; public: Car() { cout << "CA"; odometer = 0; fuel = 0; } ~Car() { cout << '~'; display(); } void operator+=(double f) { fuel += f; } virtual void drive(double speed, double time) { double distance = speed * time; if (fuel > distance * F_RATE) fuel -= distance * F_RATE; else distance = fuel / F_RATE; odometer += distance; } virtual void display() const { cout << '\n' << odometer << '|' << int(fuel); } }; class Hybrid : public Car { int gauge; double charge; public: Hybrid() { cout << "HY"; gauge = 0; charge = 0.0; } ~Hybrid() { cout << '\n' << gauge << '|' << charge << '%'; } void drive(double speed, double time) { if (speed > CUTOFF) { Car::drive(speed, time); charge += CUTOFF * time * C_RATE; } else if (charge > speed * time * U_RATE) { charge -= speed * time * U_RATE; gauge += speed * time; } else { double eTime = charge / (U_RATE * speed); gauge += speed * eTime; charge = 0; time -= eTime; Car::drive(speed, time); charge += speed * time * C_RATE; } } void display() const { Car::display(); cout << ':' << charge; } }; void drive(Car& car, double speed, double time) { car.drive(speed, time); car.display(); } void fill(Car& car, double fuel) { car += fuel; car.display(); } void show(Car car) { car.display(); } int main() { Car car; Hybrid hybrid; cout << "\n--------"; fill(car, 50); fill(hybrid, 50); cout << "\n--------"; drive(car, 60, 1); drive(hybrid, 60, 1); cout << "\n--------"; drive(car, 10, 3); drive(hybrid, 10, 3); cout << "\n--------"; show(car); show(hybrid); cout << "\n--------"; return 0; }```