Create the Train Header File
In this part of the assignment, you will create the interface for the Train class. Just as we did for the Car class, we will define the interface in this step, create stubs in the next step, and worry about the implementation after we have a proper skeleton.
Implementation Steps
Create the Train header file
Create a file named train.hpp, and then,
- Add appropriate include guards to prevent the
Trainclass from being included twice. - Since your
Trainwill be made up ofCars, you should include the header file for theCarclass. - Add include files for the C++ functionality you use (just like you did in
car.hpp). -
Before you declare the
Trainclass, declare anenumtype to distinguish different kinds of trains:enum traintype { BASIC, SMART };- An “enumerated” type in C++ is declared using the keyword
enum. - The type is used to create a finite and ordered (enumerated) list of options that the programmer can use to distinguish different cases.
- Quinn has asked us to explore two different ways of changing the size of the train when it becomes full and needs more space to hold packages or when there is a lot of empty space in the train and it makes sense to use a smaller train.
- We are going to ask you to implement a simple algorithm and then an improved algorithm, so we will talk about
BASICtrains andSMARTtrains.
- An “enumerated” type in C++ is declared using the keyword
Declare the Data Members
Your train class should have the following private data members:
-
A
Carpointer calledcars_that will later be used to hold the memory address of a dynamically allocated array ofCars in the heap. -
The following
size_tdata members:numCars_to keep track of how many cars are in the train.usage_to keep track of how much space is used by packages in the train.
-
The following
doubledata members:revenue_to keep track of how much money is accumulated .operatingCost_to keep track of how much money is spent moving boxes around.
-
The following class-wide compile-time
size_tconstants (i.e., declaredstatic constexpr)Constant Name Value Details BASIC_SIZE_CHANGE1Indicates the additive increase/decrease in size for a BASICtrain when it grows/shrinks.SMART_SIZE_CHANGE2Indicates the multiplicative increase/decrease in size for a SMARTtrain when it grows/shrinks.SMART_DOWNSIZE_THRESHOLD4When a SMARTtrain is atcapacity/SMART_DOWNSIZE_THRESHOLD, we’ll decide to remove some train cars. -
The following class-wide compile-time
doubleconstants (i.e., declaredstatic constexpr)Constant Name Value Details HANDLING_COST1.0Indicates that moving packages around costs Quinn $1.00 for each package. SHIPPING_COST4.0Indicates that Quinn makes $4.00 for every package that is shipped by a customer. -
A
traintypenamedtype_to determine if the train is aBASICtrain or aSMARTtrain. It should look like this:traintype type_;
Declare the Member Functions
-
You should have these private member functions for the
Trainclass, none of which return a value (i.e., all arevoidfunctions).Function Name Details changeSize(size_t size)Change the size of a train. upsizeIfNeeded()Check if the train needs to increase in size and then doing so if necessary. downsizeIfNeeded()Check if the train needs to decrease in size and then doing so if necessary. loadPackage()Helper function to move packages onto a train when shipped or moving packages from train to train when changing train sizes. -
Your train class should have the following public functions.
Function Name Return Type Details Train(traintype type)N/A Parameterized constructor. ~Train()N/A Destructor. addPackage()voidAdd a single package to the train when a package is sent. removePackage()voidRemove a single package to the train when a package is received. printToStream(std::ostream& outStream)voidOutput a string representation of the train to outStream. Doesn't modify the train.You should explicitly disable the following functions:
Function Name Return Type Details Train()N/A Default constructor. Train(const Train& other)N/A Copy constructor. operator=(const Train& other)Train&Copy assignment operator. -
You should also declare a function
operator<<outside the class to print out aTrainobject, just like you did incar.hpp. You may want to refer back to yourCarheader file.
If a member function doesn't modify the object, it should be declared
const. Remember, in this caseconstgoes on the end, after the close parentheses of the parameters.
Also, be sure to declare the
operator<<function outside the class, just as you did incar.hpp!
But it should work on
Trainobjects, notCarobjects!
(When logged in, completion status appears here.)