CS 70

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,

  1. Add appropriate include guards to prevent the Train class from being included twice.
  2. Since your Train will be made up of Cars, you should include the header file for the Car class.
  3. Add include files for the C++ functionality you use (just like you did in car.hpp).
  4. Before you declare the Train class, declare an enum 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 and SMART trains.

Declare the Data Members

Your train class should have the following private data members:

  1. A Car pointer called cars_ that will later be used to hold the memory address of a dynamically allocated array of Cars in the heap.

  2. 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.
  3. 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.
  4. The following class-wide compile-time size_t constants (i.e., declared static 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 at capacity/SMART_DOWNSIZE_THRESHOLD, we’ll decide to remove some train cars.
  5. The following class-wide compile-time double constants (i.e., declared static 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.
  6. A traintype named type_ to determine if the train is a BASIC train or a SMART train. It should look like this:

    traintype type_;
    

Declare the Member Functions

  1. You should have these private member functions for the Train class, none of which return a value (i.e., all are void 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.
  2. 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.
  3. You should also declare a function operator<< outside the class to print out a Train object, just like you did in car.hpp. You may want to refer back to your Car header file.

  • LHS Cow speaking

    If a member function doesn't modify the object, it should be declared const. Remember, in this case const goes on the end, after the close parentheses of the parameters.

  • RHS Cow speaking

    Also, be sure to declare the operator<< function outside the class, just as you did in car.hpp!

  • LHS Cow speaking

    But it should work on Train objects, not Car objects!

To Complete This Part of the Assignment…

You'll know you're done with this part of the assignment when you've done all of the following:

(When logged in, completion status appears here.)