In: Computer Science
Complete the following task in C++. Separate your class into header and cpp files. You can only useiostream, string and sstream. Create a main.cpp file to test your code thoroughly.
Given : commodity.h and commodity.cpp here -> https://www.chegg.com/homework-help/questions-and-answers/31-commodity-request-presented-two-diagrams-depicting-commodity-request-classes-form-basic-q39578118?trackid=uF_YZqoK
Create :
locomotive.h, locomotive.cpp, main.cpp
Locomotive Class Hierarchy
Presented here will be a class diagram depicting the nature of the class hierarchy formed
between a parent locomotive class and its children, the two kinds of specic trains
operated. The relationship is a strict one to many relationship with each specic class of
locomotive being a direct descendent of the locomotive class. The inheritance type is
public in all cases.
The locomotive class is the parent class which the other 2 are descended
from DieselElectric and Steam . In particular it is also an abstract class, having a pure virtual function and is not
intended to be instantiated. Note that only salient features are captured in the above
class diagram. For full specics on each of the classes, refer below. The class specics
of the subclasses are discussed below with a UML specication and details about what
specic behaviour, functions and operators each class needs to implement.
3.2.1 Locomotive Parent Class
locomotive
-name:string
-topSpeed:double
-maxCars:int
-tonLimit:int
-cars:commodity**
---------------------------
+locomotive()
+locomotive(name:string, topSpeed:double ,maxCars:int,tonLimit:int)
+getName() const:string
+setName(s:string):void
+getTopSpeed() const:double
+getMaxCars() const:int
+getTonLimit() const:int
+setTopSpeed(s:double):void
+setMaxCars(s:int):void
+setTonLimits(s:int):void
+getCurrentTopSpeed():double
+printManifest():void
+addCar(c:commodity*):int
+removeCar(s:string):int
+unassignCars():void
+getFirstCar():commodity *
+getCurrentTons():int
+getCurrentCars():int
+~locomotive()
+generateID()=0:string
+calculateRange():double
+getType():string
The variables are as follows:
name: The name of the locomotive. This is unique.
topSpeed: The maximum speed the locomotive can do unlimbered by any cars in
km.
maxCars: The total number of cars the locomotive can pull safely.
tonLimit: The number of tons the locomotive can carry in total across all of its
cars.
cars: The list of cars it pulls, each carrying one kind of specic commodity.
The functions are as follows:
locomotive(): The default constructor which should be empty.
The setMaxCars setter should initialise the memory of the cars variable if it has not
been set by that point.
locomotive(name:string , topSpeed:double ,maxCars:int,tonLimit:int ): The value
constructor for the class. It should allocate memory for the cars variable but leave
it unlled with nulls.
locomotive: This is the default destructor for the class. It is virtual.
generateID: This is a pure virtual function in this class. It will generate a unique
and specic id for each train class. See the specic classes for more detail on creating
it.
getter and setter: Each of the variables of the class has a getter and a setter assigned
to it that simply returns or overwrites the variable's value.
getType: This function returns the type of the locomotive as a string. The base
class type should be set to locomotive. It is virtual and the children should override
it to return dieselElectric and steam respectively.
getCurrentTons(): This function calculates the current number of tons being carried
by the locomotive and returns it.
getFirstCar(): This function returns the rst car held by the train.
getCurrentCars(): This function returns the current number of cars attached to the
train.
getCurrentTopSpeed(): This function calculates and returns the current top speed
potential for the train based on the load it is carrying. The new speed is calculated
based on the following equation:
newSpeed = oldSpeed - (1,7 * numberOfPulledCars) - (1,01*(pulledTons/10.0)
Do not use the maximums of either the pulled tons or number of cars, you need to
determine how many cars and tons the train is actually carrying to factor in the
correct speed. Additionally, if the train is overburdened, that is carrying more than
it can actually pull so it has a negative speed, the returned speed is 0.
printManifest():This function prints out the contents of the train's cars in the fol-
lowing format (assuming 2 cars for an example):
Train: Blue Jay
Car 1: Iron Ore
Car 2: Unprocessed Lumber
Total Tons: 300
The train's name is printed first, followed by the commodity in each of the cars.
The final ton total is calculated as well. Put your new lines at the end of each line.
calculateRange(): This virtual function calculates the actual eective range of the
train in terms of how far it can go on a single load of fuel. Each train uses a
dierent method of fuel storage so see each separate subclass for more information.
By default, this should be return 0.
addCar(c:commodity*): This adds a car at the rst open space in the cars array. It
returns the index of the addition. If it is not possible to add it, the function returns
-1. Cars cannot be added if they would otherwise violate the ton or car limits.
removeCar(s:string): This removes (deallocates the memory) a car with the id
passed in. It returns with the index of the car that was removed. If it is not
possible to add it, the function returns -1.
unassignCars(): This removes all cars (deallocates the memory) of all cars in the
locomotive.
#include "locomotive.h"
locomotive::locomotive()
{
}
locomotive::locomotive(string name, double topSpeed, int
maxCars, int tonLimit)
{
this->name = name;
this->topSpeed = topSpeed;
this->maxCars = maxCars;
this->tonLimit = tonLimit;
this->cars = new commodity *[maxCars];
for (int i = 0; i < maxCars; i++)
this->cars[i] = NULL;
}
locomotive::~locomotive()
{
for (int i = 0; i < this->maxCars; i++)
{
if (this->cars[i] !=
NULL)
{
commodity * tmp = this->cars[i];
this->cars[i] = NULL;
}
}
}
string locomotive::getName() const
{
return this->name;
}
void locomotive::setName(string s)
{
this->name = s;
}
double locomotive::getTopSpeed() const
{
return this->topSpeed;
}
int locomotive::getMaxCars() const
{
return this->maxCars;
}
int locomotive::getTonLimit() const
{
return this->tonLimit;
}
void locomotive::setTonLimit(int s)
{
this->tonLimit = s;
}
void locomotive::setTopSpeed(double s)
{
this->topSpeed = s;
}
void locomotive::setMaxCars(int s)
{
this->maxCars = s;
if (this->cars == NULL)
{
this->cars = new
commodity *[maxCars];
for (int i = 0; i <
maxCars; i++)
this->cars[i] = NULL;
}
}
int locomotive::getCurrentTons()
{
int total_ton=0;
for (int i = 0; i < this->maxCars; i++)
{
if (this->cars[i] !=
NULL)
{
total_ton += this->cars[i]->getQuantity();
}
}
return total_ton;
}
int locomotive::getCurrentCars()
{
int total_car = 0;
for (int i = 0; i < this->maxCars; i++)
{
if (this->cars[i] !=
NULL)
{
total_car ++;
}
}
return total_car;
}
commodity *locomotive :: getFirstCar()
{
for (int i = 0; i < this->maxCars; i++)
{
if (this->cars[i] !=
NULL)
{
return
this->cars[i];
}
}
return NULL;
}
double locomotive::calculateRange()
{
// specific to subclass
return 0.0;
}
int locomotive::addCar(commodity *c)
{
int i;
if (getCurrentTons() + c->getQuantity() >
getTonLimit()) return -1;
for (i = 0; i <
this->maxCars; i++)
{
if
(this->cars[i] == NULL)
{
this->cars[i] = c;
break;
}
}
if (i == this->maxCars)
return -1;
return i;
}
int locomotive::removeCar(string s)
{
int flag = -1;
for (int i = 0; i < this->maxCars; i++)
{
if (this->cars[i] !=
NULL)
{
if
(this->cars[i]->getName() == s)
{
commodity * tmp = this->cars[i];
this->cars[i] = NULL;
flag = i;
break;
}
}
}
return flag;
}
void locomotive::unassignCars()
{
for (int i = 0; i < this->maxCars; i++)
{
if (this->cars[i] !=
NULL)
{
commodity * tmp = this->cars[i];
this->cars[i] = NULL;
}
}
}
void locomotive:: printManifest()
{
int count=1;
cout << "Train: " << this->name
<< endl;
for (int i = 0; i < this->maxCars; i++)
{
if (this->cars[i] !=
NULL)
{
cout
<< "Car " << count << ": " <<
this->cars[i]->getName() << endl;
count++;
}
}
cout << "Total Tons: " << getCurrentTons()
<< endl;
}
double locomotive::getCurrentTopSpeed()
{
double current_speed = (getTopSpeed() -
(1.7*getCurrentCars() - (1.01*(getCurrentTons() / 10.0))));
if (getCurrentTons() > getTonLimit() ||
current_speed < 0)
return 0.0;
return current_speed;
}
string locomotive::getType()
{
return " ";
}