In: Computer Science
Complete the following task in C++. Separate your class into header and cpp files. You can only use iostream, 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
Given : locomotive.h, locomotive.cpp, main.cpp -> https://www.chegg.com/homework-help/questions-and-answers/complete-following-task-c--separate-class-header-cpp-files-useiostream-string-sstream-crea-q39733428
Create : DieselElectric.cpp DieselElectric.h
DieselElectric
dieselElectric
-fuelSupply:int
---------------------------
+getSupply():int
+setSupply(s:int):void
+dieselElectric(f:int)
+~dieselElectric()
+generateID():string
+calculateRange():double
The class variables are as follows:
fuelSuppply: The fuel supply of the train in terms of a number of kilolitres of diesel
fuel.
The class methods are as follows:
Getter and Setter: The getter and setter for the sundry class variable.
dieselElectric(f:int): The constructor for the class. It should instantiate the class
based on the given parameters.
dieselElectric(): The destructor for the class. It should deallocate any memory
assigned to the class. Additionally, it should (as the rst thing it does) print "Diesel
Electric Train: Name" with a newline at the end. Name" in this case refers to the
train's name assigned to it.
generateID(): This function generates a unique ID for the train by producing a
string that has the following structure:
- The start of the string is the type of train.
- Append the first 3 letters from the first commodity
-Append the total tonnage carried by the train to the end
Each should be separated by a "-" So for example,
dieselElectric-Coa-1000
You can assume there will be at least one commodity when this function is called.
calculateRange(): This calculates and returns the range of the train using the fol-
lowing equation:
Range=(fuelSupply *3.5)- 5*(avgTonsPerCar)
If the range is negative, then the range should be set to 0. The average tons per car
only considers the current number of cars and tons associated with those cars.
DieselElectric.h
//----------------------------------------------------------------------------------------------------
#ifndef DIESELELECTRIC_H
#define DIESELELECTRIC_H
#ifndef LOCOMOTIVE_H
#include"locomotive.h"
#endif
class dieselElectric:public locomotive
{
public:
int fuelSupply;
int getSupply();
void setSupply(int s);
dieselElectric(int f);
~dieselElectric();
string generateID();
double calculateRange();
};
#endif
//--------------------------------------------------------------------------------------
DieselElectric.cpp
//--------------------------------------------------------------------------------------
#ifndef DIESELELECTRIC_H
#include"DieselElectric.h"
#endif
int dieselElectric::getSupply(){
return fuelSupply;
}
void dieselElectric::setSupply(int s)
{
fuelSupply = s;
}
dieselElectric::dieselElectric(int f)
{
fuelSupply = f;
}
dieselElectric::~dieselElectric()
{
int s = getMaxCars();
//for (int i = 0; i < s; i++)
// if(cars[i]!=NULL) delete(cars);
unassignCars();
cout << "Diesel Electic Train: " <<
getName() << endl;
}
string dieselElectric::generateID()
{
string type = "dieselElectric-";
commodity *first_car = getFirstCar();
string first_name = first_car->getName();
type = type + first_name[0] + first_name[1] +
first_name[2] + '-';
int ton = getCurrentTons();
type = type + to_string(ton);
return type;
}
double dieselElectric::calculateRange()
{
double range = (3.5*fuelSupply) - 5 *
((double)getCurrentTons() / getCurrentCars());
if (range < 0) return 0;
return range;
}
//-------------------------------------------------------------------------------------
main.cpp
//---------------------------------------------------------------------------------------
#include "DieselElectric.h"
int main()
{
commodity *c = new commodity("abcs",10);
dieselElectric *diesel_train = new
dieselElectric(100);
diesel_train->setName("fastest");
diesel_train->setMaxCars(5);
diesel_train->setTonLimit(200);
diesel_train->setTopSpeed((double)50.0);
int a = diesel_train->addCar(c); //adding car
commodity d("cab", 50);
a = diesel_train->addCar(&d); //adding
car
commodity e("tata", 20);
a = diesel_train->addCar(&e);
//adding car
commodity f("hero", 70);
//a = diesel_train->addCar(&f); //adding
car
commodity g("last", 40);
a = diesel_train->addCar(&g); //adding car
cout << "Current Numbers of cars: " <<
diesel_train->getCurrentCars() << endl;
cout << "Current Ton amount : " <<
diesel_train->getCurrentTons() << endl;
cout << "Train ID: " <<
diesel_train->generateID() << endl;
cout << "Range: " <<
diesel_train->calculateRange() << endl;
//supply value changed such that range will become
zero
diesel_train->setSupply(50); //supply changed
changed;
//removed first car to change ID
cout << "removed car with position: " <<
diesel_train->removeCar("abcs") << endl;
cout << "First car name: " <<
diesel_train->getFirstCar()->getName() << endl;
cout << "Current Numbers of cars: " <<
diesel_train->getCurrentCars() << endl;
cout << "Current Ton amount : " <<
diesel_train->getCurrentTons() << endl;
cout << "Train ID: " <<
diesel_train->generateID() << endl;
cout << "Range: " <<
diesel_train->calculateRange() << endl;
delete(diesel_train);
system("pause"); // if this line through any error you can remove ,
//this is just to pause to check output on console window
return 0;
}
//--------------------------------------------------------------------------------------
Sample output:
I have tested all implemented function with all cases using above main.cpp