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 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.

Solutions

Expert Solution

#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 " ";
}


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 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...
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