Academic Integrity: tutoring, explanations, and feedback — we don’t complete graded work or submit on a student’s behalf.

Objectives Review how to create classes Learn to overload operators to work with

ID: 3721912 • Letter: O

Question

Objectives

Review how to create classes

Learn to overload operators to work with a custom class

Learn how to build a class using aggregation

Instructions:

For this assignment you are tasked with simulating an Uber driver. To complete this task we will simplify a car to something that has a fuel gauge and a speedometer. We will neglect all of the mechanical details that allow a real car to work the way they work. The description below is the minimum set of requirement you need to follow.

The FuelGauge Class: This class will simulate a fuel gauge. Its responsibilities are:

   Keep track of the car's current amount of fuel, in gallons (int)

   Reports the car's current amount of fuel, in gallons

      This MUST be done via operator overloading

   Increment the amount of fuel by 1 gallon. This simulated putting fuel in the car. (The car can hold a maximum of 15 gallons.)

      This MUST be done via operator overloading

   Decrement the amount of fuel by 1 gallon, if the amount of fuel is greater than 0 gallons. This simulates burning fuel as the car runs

      This MUST be done via operator overloading

   The construction of a fuel gauge creates a fuel gauge with a full tank

The Odometer Class: This class will simulate the car's odometer. Its responsibilities are:

   Keep track of the car's current mileage (int)

   Reports the car's current mileage

      This MUST be done via operator overloading >>

   Increments the current mileage by 1 mile.  The maximum mileage the odometer can store is 999,999 miles. When the amount is exceeded, the odometer resets the current mileage to 0.

      This MUST be done via operator overloading

   Works with a FuelGauge object. It should decrease the FuelGauge object's current amount of fuel by 1 gallon for every 24 miles traveled. (The car's fuel economy is 24 mile per gallon.)

   The construction of a odometer requires a reference to the FuelGauge that it works with (FuelGauge*).

The Car Class: This class will simulate the car. Its responsibilities are:

   Keep track of the car's fuel gauge (FuelGauge*)

   Keep track of the car's odometer (Odometer*)

   Reports the number of miles drive and how much gas remains in the car.

      This MUST be done with via operator overloading >>

   Drive a certain amount of miles while consuming gas. Works with the fuel gauge and odometer.

   The construction of a car dynamically allocates space for a fuel gauge and a odometer.

Finally create a main program to simulate a Uber driving to 5 different locations, after drive to each location the driver's car reports the miles driven and the number of gallons of gas left, the mileage to the different locations are as follows:

   10 miles away

   30 miles away

   100 miles away

   200 miles away

   22 miles away

Sample output:

Go a little extra:

Instead of having the schedule list of locations as described above, make your program more interactive, by doing the follow.

   Ask how far away is customer?

   Allow the user to make this determination

   Ask how far does customer need to go

   Allow the user to make this determination

   After every pick-up and drop-off report the number of miles driven and how much gas remains in the tank

   And drive until the Uber is out of gas

Sample output:

Drove 10 miles now I have 15 gallons left. rove 40 miles now I have 14 gallons left. rove 140 miles now I have 10 gallons left. rove 340 miles now I have 1 gallons left. Drove 360 miles I'm out of gas. Process returned (ex execution time :0.352s Press any key to continue.

Explanation / Answer

The main class would be:

#include <iostream>  
#include <cstdlib>  
#include "FuelGauge.h"
#include "Odometer.h"

using namespace std;
int main()
{
FuelGauge fuelG(15);
Odometer odm(0,&fuelG);

while (fuelG.getCurrentAmountOfFuel() > 0)
{
odm.incrementcurrentMileage();
cout << "Mileage: "<< cout<<"Fuel level"<< fuelG.getCurrentAmountOfFuel()<<" gallons"<< end1;
}
return 0;
}

The header files could be created as:

Header for Fuel Guage.

#pragma once
#include <iostream>
using namespace std;
class FuelGauge
{
private:
int currentAmountOfFuel;
public:
FuelGauge(int gallons)
{
currentAmountOfFuel=gallons;
}
//FuelGauge();
int getCurrentAmountOfFuel()
{
return currentAmountOfFuel;
}

void incrementFuelTank()
{
if (currentAmountOfFuel< 15 )
currentAmountOfFuel++;
}

void decrementFuelTank()
{
if(currentAmountOfFuel>0)
currentAmountOfFuel--;
}
};

Header for Odometer....

#pragma once
#include <iostream>
#include "FuelGauge.h"
class Odometer
{
private:
int currentMileage;
FuelGauge *fuelG;
public:
Odometer(int miles,FuelGauge *f)
{
currentMileage = miles;
fuelG= f;
}
int getCurrentMileage()
{
return currentMileage;
}

void incrementcurrentMileage()
{
if(currentMileage < 999999 )
currentMileage++;
else
currentMileage = 0;
}

void decrementcurrentMileage()
{
if (currentMileage > 24 )
currentMileage--;
(*fuelG).decrementFuelTank();
}
};

See that you give the input clearly for correct excecution of code.