Objectives In C++ Review how to create classes Learn to overload operators to wo
ID: 3722278 • Letter: O
Question
Objectives
In C++
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
#include<iostream>
#include<stdlib.h>
#define MAXG 15
#define MAXM 999999
using namespace std;
// Class FuelGauge definition
class FuelGauge
{
// Data member
static int gallons;
public:
// Overloading -- operator
void operator --()
{
--gallons;
}// End of function
// Function to return gallons
int getGallons()
{
return gallons;
}// End of function
};// End of class
// Initializes static data member
int FuelGauge::gallons = MAXG;
// Class Odometer definition
class Odometer
{
public:
// Data member
static int currentMileage;
// Overloads ++ operator
void operator ++()
{
// Increase the current mileage by one
++currentMileage;
// Checks if the current mileage is equals to maximum mileage the set it to zero
if(currentMileage == MAXM)
currentMileage = 0;
}// End of function
// Function to return current mileage
int getCurrentMileage()
{
return currentMileage;
}// End of function
};// End of class
// Initializes static data member
int Odometer::currentMileage = 0;
// Class car definition
class Car
{
public:
// Declares object as data member using deligation
FuelGauge *fg;
Odometer *om;
// Overloads >> operator
friend istream & operator >>(istream &is, Car &c)
{
int no;
// Loops till valid mileage entered by the user
do
{
// Accepts mileage
cin>>no;
// Checks if the mileage is zero or negative show error message
if(no <= 0)
cout<<" Invalid response, mileage should be greater than 0. Please reenter data.";
// Otherwise come out of the loop
else
break;
}while(1);
// Loops till no
for(int x = 0; x < no; x++)
// Increase the mileage by one
++*c.om;
// Checks if the current mileage is greater than or equals to 24 and entered mileage is less than 24
if(c.om->getCurrentMileage() >= 24 && no < 24)
// Decrement by one
--*c.fg;
// Otherwise
else
{
// Calculate the remainder
int rem = (no / 24);
// Loops till remainder
for(int x = 0; x < rem; x++)
// Decrease by one
--*c.fg;
}// End of else
// return istream object
return is;
}// End of function
// Overload << operator
friend ostream & operator <<(ostream &os, Car &c)
{
// Checks if the current gallon is less than or equals to zero
if(c.fg->getGallons() <= 0)
{
// Display message and stop
cout<<" Drove "<<c.om->getCurrentMileage()<<" miles I'm out of gas.";
exit(0);
}// End of if condition
// Otherwise display total mileage traveled and fuel left
else
cout<<" Drove "<<c.om->getCurrentMileage()<<" miles now I have "<<c.fg->getGallons()<<" gallons left.";
return os;
}// End of function
};// End of class
// main function definition
int main()
{
// Creates car object
Car cc;
// Loops till fuel available
do
{
// Accepts data and displays data using object
cout<<" How far away is customer #"<<counter<<"? ";
cin>>cc;
cout<<cc;
cout<<" How far does customer #"<<counter<<" need to go?";
cin>>cc;
cout<<cc;
}while(1);// End of do - while
}// End of main function
Sample Run:
How far away is customer #1? -5
Invalid response, mileage should be greater than 0.
Please reenter data.23
Drove 23 miles now I have 15 gallons left.
How far does customer #1 need to go?1
Drove 24 miles now I have 14 gallons left.
How far away is customer #2? 200
Drove 224 miles now I have 6 gallons left.
How far does customer #2 need to go?100
Drove 324 miles now I have 2 gallons left.
How far away is customer #3? 100
Drove 424 miles I'm out of gas.
Related Questions
Navigate
Integrity-first tutoring: explanations and feedback only — we do not complete graded work. Learn more.