main.cc file #include #include #include "train.h" int main() {   // Creates a train, with the locomotive at the front of the train.   // LinkedList diagram:   //   Locomotive -> First Class -> Business Class -> Cafe Car -> Carriage 1 ->   //   Carriage 2   std::shared_ptr carriage2 = std::make_shared(100, 100, nullptr);   std::shared_ptr carriage1 =       std::make_shared(220, 220, carriage2);   std::shared_ptr cafe_car =       std::make_shared(250, 250, carriage1);   std::shared_ptr business_class =       std::make_shared(50, 50, cafe_car);   std::shared_ptr first_class =       std::make_shared(20, 20, business_class);   std::shared_ptr locomotive =       std::make_shared(1, 1, first_class);   std::cout << "Total passengers in the train: ";   // =================== YOUR CODE HERE ===================   // 1. Print out the total number of passengers in the   //    train by invoking TotalTrainPassengers on the   //    first carriage in the train (the locomotive).   // ======================================================   std::cout << std::endl;   std::cout << "Max passengers in a carriage: ";   // =================== YOUR CODE HERE ===================   // 2. Print out the maximum number of passengers in a   //    single carriage in the train by invoking   //    MaxCarriagePassengers.   // ======================================================   std::cout << std::endl;   // =================== YOUR CODE HERE ===================   // 3. Using IsTrainFull, check if the locomotive is full.   //    If IsTrainFull returns true, print   //          "The train is full."   //    If IsTrainFull returns false, print   //          "The train has available seats."   // ======================================================   std::cout << std::endl;   // =================== YOUR CODE HERE ===================   // 4. Create a new std::shared_ptr. You may name   //    it the `caboose`, which refers to the carriage at   //    the end of a train.   //    Set its passenger count to 40, seat count to   //    75, and next carriage to `nullptr`.   // ======================================================   std::cout << "Adding a new carriage to the train!" << std::endl;   // =================== YOUR CODE HERE ===================   // 5. Using `AddCarriageToEnd`, add the new Train you've   //    created in #4 to the end of the `locomotive`.   // ======================================================   // =================== YOUR CODE HERE ===================   // 6. Using IsTrainFull, check if the locomotive is full.   //    If IsTrainFull returns true, print   //          "The train is now full."   //    If IsTrainFull returns false, print   //          "The train now has available seats."   // ======================================================   std::cout << std::endl;   return 0; } train.cc file #include "train.h" // ========================= YOUR CODE HERE ========================= // This implementation file (train.cc) is where you should implement // the member functions declared in the header (train.h), only // if you didn't implement them inline within train.h. // // Remember to specify the name of the class with :: in this format: //     MyClassName::MyFunction() { //        ... //     } // to tell the compiler that each function belongs to the Train class. // =================================================================== train.h file #include #include class Train {  public:   Train(int passenger_count, int seat_count,         std::shared_ptr next_carriage)       : passenger_count_(passenger_count),         seat_count_(seat_count),         next_carriage_(next_carriage) {}   int GetPassengerCount() const {     return passenger_count_;   }   int GetSeatCount() const {     return seat_count_;   }   std::shared_ptr GetNextCarriage() const {     return next_carriage_;   }   // ====================== YOUR CODE HERE ======================   // Define the following recursive member functions, according   // to the instructions in the README:   //   //   1. TotalTrainPassengers   //   2. MaxCarriagePassengers   //   3. IsTrainFull   //   4. AddCarriageToEnd   // ============================================================  private:   int passenger_count_;   int seat_count_;   std::shared_ptr next_carriage_; }; Output: Total passengers in the train: 641 Max passengers in a carriage: 250 The train is full. Adding a new carriage to the train! The train now has available seats.

Computer Networking: A Top-Down Approach (7th Edition)
7th Edition
ISBN:9780133594140
Author:James Kurose, Keith Ross
Publisher:James Kurose, Keith Ross
Chapter1: Computer Networks And The Internet
Section: Chapter Questions
Problem R1RQ: What is the difference between a host and an end system? List several different types of end...
icon
Related questions
Question
100%

main.cc file

#include <iostream>
#include <memory>

#include "train.h"

int main() {
  // Creates a train, with the locomotive at the front of the train.
  // LinkedList diagram:
  //   Locomotive -> First Class -> Business Class -> Cafe Car -> Carriage 1 ->
  //   Carriage 2
  std::shared_ptr<Train> carriage2 = std::make_shared<Train>(100, 100, nullptr);
  std::shared_ptr<Train> carriage1 =
      std::make_shared<Train>(220, 220, carriage2);
  std::shared_ptr<Train> cafe_car =
      std::make_shared<Train>(250, 250, carriage1);
  std::shared_ptr<Train> business_class =
      std::make_shared<Train>(50, 50, cafe_car);
  std::shared_ptr<Train> first_class =
      std::make_shared<Train>(20, 20, business_class);
  std::shared_ptr<Train> locomotive =
      std::make_shared<Train>(1, 1, first_class);

  std::cout << "Total passengers in the train: ";
  // =================== YOUR CODE HERE ===================
  // 1. Print out the total number of passengers in the
  //    train by invoking TotalTrainPassengers on the
  //    first carriage in the train (the locomotive).
  // ======================================================
  std::cout << std::endl;

  std::cout << "Max passengers in a carriage: ";
  // =================== YOUR CODE HERE ===================
  // 2. Print out the maximum number of passengers in a
  //    single carriage in the train by invoking
  //    MaxCarriagePassengers.
  // ======================================================
  std::cout << std::endl;

  // =================== YOUR CODE HERE ===================
  // 3. Using IsTrainFull, check if the locomotive is full.
  //    If IsTrainFull returns true, print
  //          "The train is full."
  //    If IsTrainFull returns false, print
  //          "The train has available seats."
  // ======================================================
  std::cout << std::endl;

  // =================== YOUR CODE HERE ===================
  // 4. Create a new std::shared_ptr<Train>. You may name
  //    it the `caboose`, which refers to the carriage at
  //    the end of a train.
  //    Set its passenger count to 40, seat count to
  //    75, and next carriage to `nullptr`.
  // ======================================================

  std::cout << "Adding a new carriage to the train!" << std::endl;
  // =================== YOUR CODE HERE ===================
  // 5. Using `AddCarriageToEnd`, add the new Train you've
  //    created in #4 to the end of the `locomotive`.
  // ======================================================

  // =================== YOUR CODE HERE ===================
  // 6. Using IsTrainFull, check if the locomotive is full.
  //    If IsTrainFull returns true, print
  //          "The train is now full."
  //    If IsTrainFull returns false, print
  //          "The train now has available seats."
  // ======================================================
  std::cout << std::endl;
  return 0;
}

train.cc file

#include "train.h"

// ========================= YOUR CODE HERE =========================
// This implementation file (train.cc) is where you should implement
// the member functions declared in the header (train.h), only
// if you didn't implement them inline within train.h.
//
// Remember to specify the name of the class with :: in this format:
//     <return type> MyClassName::MyFunction() {
//        ...
//     }
// to tell the compiler that each function belongs to the Train class.
// ===================================================================

train.h file

#include <memory>
#include <string>

class Train {
 public:
  Train(int passenger_count, int seat_count,
        std::shared_ptr<Train> next_carriage)
      : passenger_count_(passenger_count),
        seat_count_(seat_count),
        next_carriage_(next_carriage) {}

  int GetPassengerCount() const {
    return passenger_count_;
  }
  int GetSeatCount() const {
    return seat_count_;
  }
  std::shared_ptr<Train> GetNextCarriage() const {
    return next_carriage_;
  }
  // ====================== YOUR CODE HERE ======================
  // Define the following recursive member functions, according
  // to the instructions in the README:
  //
  //   1. TotalTrainPassengers
  //   2. MaxCarriagePassengers
  //   3. IsTrainFull
  //   4. AddCarriageToEnd
  // ============================================================

 private:
  int passenger_count_;
  int seat_count_;
  std::shared_ptr<Train> next_carriage_;
};

Output:

Total passengers in the train: 641
Max passengers in a carriage: 250
The train is full.
Adding a new carriage to the train!
The train now has available seats.

Implementing the Train Class
We've partially defined the Train class for you in "train.h" and provided the constructor, accessor (getter) functions, and member variables for
you for a Train: passenger_count_ representing how many passengers are in the current Train carriage, seat_count_ representing the
number of seats in the current Train carriage, and next_carriage_ representing the next Train carriage, if there is one.
You are tasked with defining the recursive functions.
1. Define the recursive member function TotalTrainPassengers, which accepts no input, and returns an int representing the total number
of passengers in the entire train (i.e. all the passengers in the current train carriage plus all the passengers in the remaining carriages).
2. Define the recursive member function MaxCarriagePassengers, which accepts no input, and returns an int representing the maximum
number of passengers in a single carriage that is part of this train.
3. Define the recursive member function IsTrainFull, which accepts no input, and returns a bool. IsTrainFull should return true if
every carriage on this train is full, i.e. the passenger count is equal to the seat count on every carriage - in other words, every seat is
occupied by a passenger. If there is an available seat anywhere in the train, IsTrainFull should return false.
4. Define the recursive member function AddCarriageToEnd, which accepts a std::shared_ptr<Train> as input. This function adds the given
Train to the very end of the train. Hint: what base case checks if the current Train is the last carriage in the train?
In main.cc, you will be asked to invoke each recursive function for a line of train carriages we've provided for you.
After running main, your output should be:
Total passengers in the train: 641
Max passengers in a carriage: 250
The train is full.
Adding a new carriage to the train!
The train now has available seats.
Transcribed Image Text:Implementing the Train Class We've partially defined the Train class for you in "train.h" and provided the constructor, accessor (getter) functions, and member variables for you for a Train: passenger_count_ representing how many passengers are in the current Train carriage, seat_count_ representing the number of seats in the current Train carriage, and next_carriage_ representing the next Train carriage, if there is one. You are tasked with defining the recursive functions. 1. Define the recursive member function TotalTrainPassengers, which accepts no input, and returns an int representing the total number of passengers in the entire train (i.e. all the passengers in the current train carriage plus all the passengers in the remaining carriages). 2. Define the recursive member function MaxCarriagePassengers, which accepts no input, and returns an int representing the maximum number of passengers in a single carriage that is part of this train. 3. Define the recursive member function IsTrainFull, which accepts no input, and returns a bool. IsTrainFull should return true if every carriage on this train is full, i.e. the passenger count is equal to the seat count on every carriage - in other words, every seat is occupied by a passenger. If there is an available seat anywhere in the train, IsTrainFull should return false. 4. Define the recursive member function AddCarriageToEnd, which accepts a std::shared_ptr<Train> as input. This function adds the given Train to the very end of the train. Hint: what base case checks if the current Train is the last carriage in the train? In main.cc, you will be asked to invoke each recursive function for a line of train carriages we've provided for you. After running main, your output should be: Total passengers in the train: 641 Max passengers in a carriage: 250 The train is full. Adding a new carriage to the train! The train now has available seats.
Expert Solution
trending now

Trending now

This is a popular solution!

steps

Step by step

Solved in 5 steps with 1 images

Blurred answer
Recommended textbooks for you
Computer Networking: A Top-Down Approach (7th Edi…
Computer Networking: A Top-Down Approach (7th Edi…
Computer Engineering
ISBN:
9780133594140
Author:
James Kurose, Keith Ross
Publisher:
PEARSON
Computer Organization and Design MIPS Edition, Fi…
Computer Organization and Design MIPS Edition, Fi…
Computer Engineering
ISBN:
9780124077263
Author:
David A. Patterson, John L. Hennessy
Publisher:
Elsevier Science
Network+ Guide to Networks (MindTap Course List)
Network+ Guide to Networks (MindTap Course List)
Computer Engineering
ISBN:
9781337569330
Author:
Jill West, Tamara Dean, Jean Andrews
Publisher:
Cengage Learning
Concepts of Database Management
Concepts of Database Management
Computer Engineering
ISBN:
9781337093422
Author:
Joy L. Starks, Philip J. Pratt, Mary Z. Last
Publisher:
Cengage Learning
Prelude to Programming
Prelude to Programming
Computer Engineering
ISBN:
9780133750423
Author:
VENIT, Stewart
Publisher:
Pearson Education
Sc Business Data Communications and Networking, T…
Sc Business Data Communications and Networking, T…
Computer Engineering
ISBN:
9781119368830
Author:
FITZGERALD
Publisher:
WILEY