Question

In: Computer Science

Complete the following task in C++. Separate your class into header and cpp files. You can...

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.

Solutions

Expert Solution

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


Related Solutions

Complete the following task in C++. Separate your class into header and cpp files. You can...
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...
Write the header and the implementation files (.h and .cpp separatelu) for a class called Course,...
Write the header and the implementation files (.h and .cpp separatelu) for a class called Course, and a simple program to test it, according to the following specifications:                    Your class has 3 member data: an integer pointer that will be used to create a dynamic variable to represent the number of students, an integer pointer that will be used to create a dynamic array representing students’ ids, and a double pointer that will be used to create a dynamic array...
You are to write a class named Rectangle. You must use separate files for the header...
You are to write a class named Rectangle. You must use separate files for the header (Rectangle.h) and implementation (Rectangle.cpp) just like you did for the Distance class lab and Deck/Card program. You have been provided the declaration for a class named Point. Assume this class has been implemented. You are just using this class, NOT implementing it. We have also provided a main function that will use the Point and Rectangle classes along with the output of this main...
WRITE ONLY THE TO DO'S   in FINAL program IN C++ (necessary cpp and header files are...
WRITE ONLY THE TO DO'S   in FINAL program IN C++ (necessary cpp and header files are witten below) "KINGSOM.h " program below: #ifndef WESTEROS_kINGDOM_H_INCLUDED #define WESTEROS_KINGDOM_H_INCLUDED #include <iostream> namespace westeros { class Kingdom{         public:                 char m_name[32];                 int m_population; };         void display(Kingdom&);                      } #endif } Kingdom.cpp Program below: #include <iostream> #include "kingdom.h" using namespace std; namespace westeros {         void display(Kingdom& pKingdom) {                 cout << pKingdom.m_name << ", population " << pKingdom.m_population << endl;                                                               FINAL:...
C++ question. Need all cpp and header files. Part 1 - Polymorphism problem 3-1 You are...
C++ question. Need all cpp and header files. Part 1 - Polymorphism problem 3-1 You are going to build a C++ program which runs a single game of Rock, Paper, Scissors. Two players (a human player and a computer player) will compete and individually choose Rock, Paper, or Scissors. They will then simultaneously declare their choices and the winner is determined by comparing the players’ choices. Rock beats Scissors. Scissors beats Paper. Paper beats Rock. The learning objectives of this...
Your primary task for this exercise is to complete header file by writing three functions with...
Your primary task for this exercise is to complete header file by writing three functions with its description below: removeAt function – to remove the item from the list at the position specified by location. Because the list elements are in no particular order (unsorted list), you could simple remove the element by swapping the last element of the list with the item to be removed and reducing the length of the list. insertAt function - to insert an item...
How do you use header files on a program? I need to separate my program into...
How do you use header files on a program? I need to separate my program into header files/need to use header files for this program.Their needs to be 2-3 files one of which is the menu. thanks! #include #include #include using namespace std; const int maxrecs = 5; struct Teletype { string name; string phoneNo; Teletype *nextaddr; }; void display(Teletype *); void populate(Teletype *); void modify(Teletype *head, string name); void insertAtMid(Teletype *, string, string); void deleteAtMid(Teletype *, string); int find(Teletype...
Create a class Student (in the separate c# file but in the project’s source files folder)...
Create a class Student (in the separate c# file but in the project’s source files folder) with those attributes (i.e. instance variables): Student Attribute Data type (student) id String firstName String lastName String courses Array of Course objects Student class must have 2 constructors: one default (without parameters), another with 4 parameters (for setting the instance variables listed in the above table) In addition Student class must have setter and getter methods for the 4 instance variables, and getGPA method...
A header file contains a class template, and in that class there is a C++ string...
A header file contains a class template, and in that class there is a C++ string object. Group of answer choices(Pick one) 1)There should be a #include for the string library AND a using namespace std; in the header file. 2)There should be a #include for the string library. 3)There should be a #include for the string library AND a using namespace std; in the main program's CPP file, written before the H file's include.
i need an example of a program that uses muliple .h and .cpp files in c++...
i need an example of a program that uses muliple .h and .cpp files in c++ if possible.
ADVERTISEMENT
ADVERTISEMENT
ADVERTISEMENT