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
Train
class from being included twice. - Since your
Train
will be made up ofCar
s, you should include the header file for theCar
class. - Add include files for the C++ functionality you use (just like you did in
car.hpp
). -
Before you declare the
Train
class, declare anenum
type 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
BASIC
trains andSMART
trains.
- 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
Car
pointer calledcars_
that will later be used to hold the memory address of a dynamically allocated array ofCar
s in the heap. -
The following
size_t
data 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
double
data 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_t
constants (i.e., declaredstatic constexpr
)Constant Name Value Details BASIC_SIZE_CHANGE
1
Indicates the additive increase/decrease in size for a BASIC
train when it grows/shrinks.SMART_SIZE_CHANGE
2
Indicates the multiplicative increase/decrease in size for a SMART
train when it grows/shrinks.SMART_DOWNSIZE_THRESHOLD
4
When a SMART
train is atcapacity/SMART_DOWNSIZE_THRESHOLD
, we’ll decide to remove some train cars. -
The following class-wide compile-time
double
constants (i.e., declaredstatic constexpr
)Constant Name Value Details HANDLING_COST
1.0
Indicates that moving packages around costs Quinn $1.00 for each package. SHIPPING_COST
4.0
Indicates that Quinn makes $4.00 for every package that is shipped by a customer. -
A
traintype
namedtype_
to determine if the train is aBASIC
train or aSMART
train. It should look like this:traintype type_;
Declare the Member Functions
-
You should have these private member functions for the
Train
class, none of which return a value (i.e., all arevoid
functions).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()
void
Add a single package to the train when a package is sent. removePackage()
void
Remove a single package to the train when a package is received. printToStream(std::ostream& outStream)
void
Output 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 aTrain
object, just like you did incar.hpp
. You may want to refer back to yourCar
header file.
If a member function doesn't modify the object, it should be declared
const
. Remember, in this caseconst
goes 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
Train
objects, notCar
objects!
(When logged in, completion status appears here.)