In: Computer Science
(Using C++ create a Car Instrument Simulator)
Car Instrument Simulator
Create the following classes: Car, Odometer, FuelGauge.
The Car class allows for the instantiation of a Car object.
The Car object will contain (via composition) an Odometer object, and a FuelGauge object.
The Car object will also include "make", "model", and "color" as string properties (instance variables).
In the Car constructor method, you will create a FuelGauge object, passing 15 as an argument into the FuelGauge constructor indicating the number of gallons.
In the Car constructor method, you will create an Odometer object, passing 0 and the address of the FuelGauge object as arguments to the Odometer constructor, indicating the number of miles driven and the address of the FuelGauge object respectively.
The Car class will have a "drive(int miles)" method.
The Odometer object will contain a pointer to a FuelGauge object.
The Odometer object will contain a "milesDriven" instance variable.
The Odometer object will have an "incrementMileage()" method.
The implementation of the Odometer incrementMileage method will
be such that if the fuel level is greater than zero, the Odometer
increments its mileage instance variable by 1. Also, at every
interval of GAS_MILEAGE (a constant that specifies the miles per
gallon), and if the FuelGauge's fuelLevel is greater than zero, the
Odometer will call the decreaseFuelLevel() method on the FuelGauge
object.
The FuelGauge object will have a "decreaseFuelLevel()" method.
The FuelGauge object will contain a "fuelLevel" instance variable.The FuelGauge object will have a "getFuelLevel()" method that returns the current value of the fuelLevel instance variable.
The FuelGauge object will have a "decreaseFuelLevel(int gallons)" method.
The implementation of the FuelGauge decreaseFuelLevel method will be such that the FuelGauge decrements the fuelLevel instance variable by the specified number of gallons. Do not decrease the fuelLevel to a negative value.
The implementation of the Car "drive(int miles)" method will be such that it has a loop wherein...
while you've driven less then the number of miles specified by the
"drive" method parameter value (make it 500) and the fuel level is
greater than zero,call the incrementMileage method on the Odometer
object.
When the car stops driving because it has run out of fuel, display
the number of miles driven.
Deliverables:
7 files: a .h file for each class, a .cpp file for each class, and one .cpp file for your main function.
Note: Could you plz go through this code and let me
know if u need any changes in this.Thank You
_________________
// FuelGuage.h
#ifndef FUELGUAGE_H
#define FUELGUAGE_H
class FuelGuage
{
public:
FuelGuage();
FuelGuage(int gallons);
void decreaseFuelLevel();
int getFuelLevel();
private:
// Declaring variables
int fuelLevel;
};
#endif
_____________________
// FuelGuage.cpp
#include <iostream>
using namespace std;
#include "FuelGuage.h"
void FuelGuage::decreaseFuelLevel()
{
if(fuelLevel>0)
{
fuelLevel--;
}
}
int FuelGuage::getFuelLevel()
{
return fuelLevel;
}
FuelGuage::FuelGuage(int gallons)
{
this->fuelLevel=gallons;
}
FuelGuage::FuelGuage()
{
}
___________________
// Odometer.h
#ifndef ODOMETER_H
#define ODOMETER_H
#include "FuelGuage.h"
class Odometer
{
public:
Odometer();
Odometer(int milesDriven,FuelGuage *fg);
void incrementMileage();
private:
// Declaring variables
FuelGuage *fg;
int milesDriven;
static const int GAS_MILEAGE=15;
};
#endif
____________________
// Odometer.cpp
#include <iostream>
using namespace std;
#include "Odometer.h"
#include "FuelGuage.h"
Odometer::Odometer(int milesDriven,FuelGuage *fg)
{
this->milesDriven=milesDriven;
this->fg=fg;
}
void Odometer::incrementMileage()
{
if(fg->getFuelLevel()>0)
{
milesDriven++;
cout<<"Mileage :"<<milesDriven<<endl;
cout<<"Fuel Level
:"<<fg->getFuelLevel()<<endl;
cout<<"------------------------------------"<<endl;
}
if(milesDriven%GAS_MILEAGE==0)
{
fg->decreaseFuelLevel();
}
}
Odometer::Odometer()
{
}
_______________________
// Car.h
#ifndef CAR_H
#define CAR_H
#include "FuelGuage.h"
#include "Odometer.h"
class Car
{
public:
Car();
void drive(int miles);
private:
// Declaring variables
FuelGuage fg;
Odometer odo;
string make;
string model;
int year;
};
#endif
________________________
// Car.cpp
#include <iostream>
using namespace std;
#include "Car.h"
#include "Odometer.h"
#include "FuelGuage.h"
Car::Car()
{
FuelGuage f(15);
this->fg=f;
Odometer odo(0,&fg);
this->odo=odo;
}
void Car::drive(int miles)
{
for(int i=1;i<=miles;i++)
odo.incrementMileage();
}
_______________________
// main.cpp
#include <iostream>
using namespace std;
#include "Car.h"
int main()
{
Car c;
c.drive(500);
return 0;
}
_________________________
Output:
_______________Could you plz rate me well.Thank You