Need help implementing the following classes in c++. I need to write a code for
ID: 656072 • Letter: N
Question
Need help implementing the following classes in c++. I need to write a code for each function and I'm having problems passing the parameters to each function and making all the functions work together. These functions will go into a header file, which later will be used in a driver program. Please read the comments in order to correctly implement the classes. Remember I need to build the functions with proper code in order to make them work.
class Tank
{
Public:
Tank(float); // creates a tank with certain capacity (taken from parameter)
virtual ~Tank(); // does nothing
void consumeFuel(float); //Decrements fuel by a value specified in the parameter. It also checks
// if the fuel gets to 0, in which case it sets empty to true.
float getFuel(); // fuel reading
virtual void refuel();
bool isEmpty();
int getNumTanks(); //In some vehicles there can be more than one gas tank. This funcion returns
// the number of tanks for that vehicle.
float getTankCapacity();
void setFuel(float fl);
protected:
static int numTanks;
const float tankCapacity;
float fuel;
bool empty;
};
class Engine
{
public:
Engine(int);
~Engine(){};
void setTrip(int tr);
int getTrip();
bool isStarted();
void start(Tank &);
void stop();
protected:
bool started;
int trip, numCyl;
};
The constructor in this class takes the number of cylinders from the argument and assigns it to the corresponding number of the class. It then initializes all other members to 0 or false as appropiate. The start function can only start the engine if the tank is not empty. The stop function cannot stop the engine that has not been started.
class Vehicle
{
public:
~Vehicle(){}; // does nothing
bool startEngine(); // This fuction checks for the number of occupants. it starts the engine only if the number of occupants is > 0; in which case, it calls the Engine's method to start and passes the tank object there.
void stopEngine(); // stops the engine.
void setOccupants(int occ);
int getOccupants();
bool engineStatus();// Checks and returns engine status if the engine was started.
double fuelMeter();
void addFuel(); // should call refuel method of the tank.
bool moving(int &, float); // Moves the car, which can only move if the engine was started, tanks are not empty, and trip is not complete. If these conditions are satisfied, function will enter a loop to move the car. In than loop, the odometer gets incremented, fuel gets decremented (by callin ghte method of tank to consume fuel. When trip gets completed or tank gets empty, the loop should exit. Function should call stop method of engine and output statistics of the trip
protected:
Vehicle(int occ, float tankCapacity, int numCyl);
int occupants;
Engine engine;
Tank tank;
private:
Vehicle(); // does nothing
};
Constructor creates a vehicle with number of occupants in it, creates a tank with capacity, and creates engine with number of cylinders. These parameters are taken from the cosntructor of the derived class, when it is called. Vehicle(int occ, float tankCapacity, int numCyl)
class policeCar : public Vehicle
{
public:
policeCar(); // Constructor takes no parameters but we need to pass parameters to the base class Vehicle.
~policeCar();
void trafficStop(Vehicle &);
};
Class policeCar has only one method, which is to do a traffic stop. When a traffic stop is made, it results in sttoping the vehicle's engine and decrementing the number of vehicle's occupants. After the traffic stop, the vehicle can move, but the vehicle should not start the engine if there is no occupants in it.
class familyCar
Here we need to declare
enum Status{notReady, ready, disabled} and
class OnStar
{
pubilc:
OnStar();
~OnStar();
Status onStarActivate();
Status status;
};
OnStar class has only one method, which is onStarActivate. This method should ask the user if help is needed, if so, return status disabled; otherwise, it returns status ready.
class familyCar
{
friend class OnStar;
friend Status onStar(FamilyCar &);
public:
familyCar(int, float, int = 4); //Creates a vehicle with default number of cylinders in engine to 4
~familiyCar(){}; // does nothign
void stopCar(); // stops the vehicle's engine.
Status airbag(OnStar &); // Calls the OnStar method onStarActivate. If method returns status disabled, the vechicle should stop.
};
class lightTruck : public Vehicle
{
public:
lightTruck(int, float, int);
~lightTruck(){};
int getLoad();
void setLoad(int);
protected:
int load;
};
class heavyTruck : public lightTruck
{
public:
heavyTruck(int, float, int);
~heavyTruck(){};
double fuelMeter(); // should give the reading of both tanks, the total of two tanks, and the current level of the working tank.
void refuel(); // should fill each tank, but not beyond its capacity. After the refuel, both tanks should be filled.
void switchTanks();// allows the user to switch tanks. The user can only swicth to the tanks that is not empty. If the user swicthes to an empty tank, the engine will stop and the OnStar should activate.
private:
Tank tank2;
};
Here the light truck is based an a family car. In addition to the family car, the light truck adds a load property. The heavy truck adds additional fuel tank, and because of that, it should override methods fuelMeter() and refuel(), inherited from Vehicle. Each type of vehicle should have different mileage.
The full implementation of these clasess should allow the user to launch a family car and a heavy truck(with load) for a trip of 100 miles. While on that trip, the vehicles got stopped by the police car( each vehicle at random miles). Both vehicles had their airbag activated (due to factory defect) which caused the OnStar system to activate and ask for the status. At the end of the trip, the program should output the statictics, which should include when the vehicles were stop by the police, when the OnStar was activated, how many times the vehicles stopped to refuel.
The implementation of the classes should work with the following test driver program:
int main()
{
Status status = ready;
bool ready = true;
bool response = false;
int trip = 50;
int i = 0;
familyCar fc(0, 12.2f, 4);
policeCar trooper;
fc.startEngine()
fc.stopEngine();
fc.setOccupants(4);
fc.startEngine();
fc.startEngine()
while(trip > 0 && status == ready)
{
if (fc.moving(trip,2))
{
if (!fc.engineStatus())
if(fc.fuelMeter() <= 2.0)
fc.addFuel();
}
if(trip <= 30 && !response)
{
reponse = true;
status = fc.airbag();
}
if (trip <= 20)
{
trooper.trafficStop(fc);
}
}
return 0;
}
Explanation / Answer
class Tank
{
Public:
void Tank(float capacity)
{tankCapacity=capacity;}
virtual ~Tank(){}
void consumeFuel(float f)
{ fuel= fuel-f;
if(fuel==0)
{empty=true;}
}
float getFuel()
{return fuel ;}
virtual void refuel()
{empty=false;}
bool isEmpty()
{if(empty==true)
return empty;}
int getNumTanks()
{return numTanks;
}
float getTankCapacity()
{return tankCapacity;}
void setFuel(float fl)
{this.fuel=fl;}
protected:
static int numTanks;
const float tankCapacity;
float fuel;
bool empty;
};
class Engine
{
public:
Engine(int c)
{numCyl=c;}
~Engine(){};
void setTrip(int tr)
{this.trip=tr;}
int getTrip()
{return this.trip;}
bool isStarted()
{if(started==true)
{return true;}
else
return false;}
void start(Tank tt)
{if(tt.isEmpty())
{started=false;
}
else
started=true;
}
void stop()
{if(isStarted)
{started=false;}
}
protected:
bool started;
int trip, numCyl;
};
class Vehicle
{
public:
~Vehicle(){}
bool startEngine()
{occupants= getOccupants ();
if(occupants>0)
{engine.start(tank);}}
void stopEngine()
{engine.stop();}
void setOccupants(int occ)
{this.occupants=occ;}
int getOccupants()
{return this.occupants;}
bool engineStatus()
{return engine.isStarted();}
double fuelMeter(){
return tank.getfuel();
}
void addFuel()
{tank.refuel();
}
bool moving(int trip, float ful)
{if(tank.isEmpty)
{return false;}
else
{ if(engine.isStarted)
{if(trip<50)
{while((trip<50)||(tank.isEmpty))
{trip=trip+1;
tank.consumeFuel(ful);
} }
engine.stop();}
else return false;
}}
protected:
Vehicle(int occ, float tankCapacity, int numCyl)
{this.occupants=occ;
engine.numCyl=numCyl;
tank.tankCapacity=tankCapacity;}
int occupants;
Engine engine;
Tank tank;
private:
Vehicle(); // does nothing
};
class policeCar : public Vehicle
{
public:
policeCar(); // Constructor takes no parameters but we need to pass parameters to the base class Vehicle.
~policeCar();
void trafficStop(Vehicle vv)
{engine.stop();
int cc=vv.getoccupants;
vv.setoccupants(cc-1);
if(vv.getoccupants>0)
{engine.start();}}
};
enum Status{notReady, ready, disabled}
class OnStar
{
pubilc:
OnStar();
~OnStar();
Status onStarActivate()
{ char ans;
cout<<
Related Questions
drjack9650@gmail.com
Navigate
Integrity-first tutoring: explanations and feedback only — we do not complete graded work. Learn more.