Question

C++ ONLY Please TRAINS DESCRIPTION You are completing a program that allows for several different types...

C++ ONLY Please

TRAINS

DESCRIPTION

You are completing a program that allows for several different types of passenger trains. One train can hold only cats. Another train can hold only wizards. You are going to create a Wizard class, a Cat class, and a Train class. You are provided the driver, lab2.cpp. The Train class should be a template class where the type of passenger is the template data type.

Specifications

cat class

Attributes:

  • name of cat
  • breed of cat
  • color of cat
  • age of cat

Functions:

  • constructor – the constructor should have 4 parameters and should initialize the attributes to this sent data.
  • overloaded << operator – whenever outside code prints out a Cat object, this function will determine which attributes will be printed and their formatting. You should print out each of the 4 pieces of data on a single line, separated by commas.

Wizard Class

Attributes:

  • name of wizard
  • age of wizard
  • height of wizard

Functions:

  • constructor – the constructor should have 3 parameters and should initialize the attributes to this sent data.
  • overloaded << operator – whenever outside code prints out a Wizard object, this function will determine which attributes will be printed and their formatting. You should print out each of the 3 pieces of data on a single line, separated by commas.

Train Template Class

Attributes:

  • name of the train
  • home location of the train
  • train passenger capacity (the maximum number of passengers allowed on the train)
  • the number of passengers currently on board
  • a pointer to a template data type. This eventually (in the constructor) will be used to create an array of the passengers.

Functions:

  • constructor – the constructor should have 3 parameters and should initialize the train name, home location, and capacity to these values. Then, the constructor should dynamically allocate an array the size of the train capacity. The number of passengers currently on board should be initialized to zero.
  • destructor – the destructor should release the dynamically allocated array
  • accessor function for the train name (getTrainName)
  • addPassenger function – this function should have a template type as a parameter. If the number of passengers on board is euql to the train passenger capacity, your code should print that no more passengers can board the train. Otherwise, your code should place this new passenger in the correct spot in the array and then increment the number of passengers on board.
  • printPassengers function – this function should go through the passengers array and print each passenger that is currently on board.

Provided Driver:

#include "Train.h"
#include "Cat.h"
#include "Wizard.h"
#include <iostream>
using namespace std;

int main()
{
   //Create a Wizard Train object
Train<Wizard*> wizardTrain("Hogwarts Express", "Kings Cross Station, London", 10);
  
   //Create wizards & then add them to the Wizard Train
Wizard* wizard1 = new Wizard("Bill", 32, 65);
wizardTrain.addPassenger(wizard1);
Wizard* wizard2 = new Wizard("Jill", 24, 45);
wizardTrain.addPassenger(wizard2);
Wizard* wizard3 = new Wizard("Will", 10, 42);
wizardTrain.addPassenger(wizard3);
Wizard* wizard4 = new Wizard("Neal", 88, 73);
wizardTrain.addPassenger(wizard4);
  
   //Print all the Wizard Train passengers
cout << "\n\nHere are the wizards who have boarded the " << wizardTrain.getTrainName() << "!\n\n";
wizardTrain.printPassengers();

cout << endl << endl;

   //Create a Cat Train object
Train<Cat*> kittyTrain("Hello Kitty Express", "Japan", 10);
  
   //Create Cats & then add them to the Cat Train
Cat* cat1 = new Cat("Yella Cat", "mixed", "orange & yellow", 8);
kittyTrain.addPassenger(cat1);
Cat* cat2 = new Cat("Mouth", "mixed", "black & white", 8);
kittyTrain.addPassenger(cat2);
Cat* cat3 = new Cat("Chloe", "siamese", "black & white", 3);
kittyTrain.addPassenger(cat3);
Cat* cat4 = new Cat("Brutus", "hairless", "skin color", 4);
kittyTrain.addPassenger(cat4);
Cat* cat5 = new Cat("Wally", "catdog", "lime", 1);
kittyTrain.addPassenger(cat5);
Cat* cat6 = new Cat("Gladass", "siamese", "black & white", 6);
kittyTrain.addPassenger(cat6);
  
   //Print all the Cat Train passengers
cout << "Here are the cats who have boarded the " << kittyTrain.getTrainName() << "!\n\n";
kittyTrain.printPassengers();

cout << endl << endl;
return 0;

}

Sample Output

After you create the three classes required for lab2.cpp, you need to test all the code to make sure you get the same output as below! There is no user input in this program.

Here are the wizards who have boarded the Hogwarts Express!

Bill, 32 years old, 65 inches tall

Jill, 24 years old, 45 inches tall

Will, 10 years old, 42 inches tall

Neal, 88 years old, 73 inches tall

Here are the cats who have boarded the Hello Kitty Express!

Yella Cat, mixed, orange & yellow, 8

Mouth, mixed, black & white, 8

Chloe, siamese, black & white, 3

Brutus, hairless, skin color, 4

Wally, catdog, lime, 1

Gladass, siamese, black & white, 6

0 0
Add a comment Improve this question Transcribed image text
Answer #1

Train.h

=============================

#pragma once
#include <iostream>
using namespace std;

template<typename T>
class Train {
private:
   string name; // name of the train
   string home; // home location of the train
   int capacity; // train passenger capacity(the maximum number of passengers allowed on the train)
   int num_of_passengers; // the number of passengers currently on board
   T* passengers; // a pointer to a template data type create an array of the passengers.
public:
   // constructor
   Train(string name, string home, int capacity) {
       // initialize the train name, home location, and capacity to these values.
       this->name = name;
       this->home = home;
       this->capacity = capacity;
       // dynamically allocate an array the size of the train capacity.
       passengers = new T[capacity];
       // The number of passengers currently on board should be initialized to zero.
       num_of_passengers = 0;
   }

   // destructor
   ~Train() {
       // release the dynamically allocated array
       delete passengers;
   }

   // returns name of the train
   string getTrainName() {
       return name;
   }

   // add a passenger to train
   void addPassenger(T& passanger) {
       // check the number of passengers on board is euql to the train passenger capacity
       if (num_of_passengers == capacity) {
           cout << "No more passengers can board the train!" << endl;
       }
       else {
           // place this new passenger in the correct spot in the array
           passengers[num_of_passengers] = passanger;
           // increment the number of passengers on board.
           num_of_passengers++;
       }
   }

   // prints all passangers on the train
   void printPassengers() {
       // go through the passengers array and print each passenger that is currently on board
       for (int i = 0; i < num_of_passengers; i++) {
           cout << *passengers[i] << endl;
       }
   }
};

====================================

Cat.h

====================================

#pragma once
#include <iostream>
using namespace std;

class Cat {
private:
   string name; // name of cat
   string breed; // breed of cat
   string color; // color of cat
   int age; // age of cat
public:
   // constructor
   Cat(string name, string breed, string color, int age) {
       // initialize variables
       this->name = name;
       this->breed = breed;
       this->color = color;
       this->age = age;
   }
   // overloaded operator to print out the Cat object
   friend ostream& operator << (ostream& os, Cat& cat) {
       // print out each of the 4 pieces of data on a single line, separated by commas.
       os << cat.name << ", " << cat.breed << ", " << cat.color << ", " << cat.age;
       return os;
   }
};

==================================

Wizard.h

=================================

#pragma once
#include <iostream>
using namespace std;

class Wizard {
private:
   string name; // name of wizard
   int age; // age of wizard
   int height; // height of wizard
public:
   // constructor
   Wizard(string name, int age, int height) {
       // initialize variables
       this->name = name;
       this->age = age;
       this->height = height;
   }
   // overloaded operator to print out the Wizard object
   friend ostream& operator << (ostream& os, Wizard& wizard) {
       // print out each of the 3 pieces of data on a single line, separated by commas.
       os << wizard.name << ", " << wizard.age << " years old, " << wizard.height << " inches tall";
       return os;
   }
};

========================

Driver.cpp

========================

#include "Train.h"
#include "Cat.h"
#include "Wizard.h"
#include <iostream>
using namespace std;

int main() {
   //Create a Wizard Train object
   Train<Wizard*> wizardTrain("Hogwarts Express", "Kings Cross Station, London", 10);

   //Create wizards & then add them to the Wizard Train
   Wizard* wizard1 = new Wizard("Bill", 32, 65);
   wizardTrain.addPassenger(wizard1);
   Wizard* wizard2 = new Wizard("Jill", 24, 45);
   wizardTrain.addPassenger(wizard2);
   Wizard* wizard3 = new Wizard("Will", 10, 42);
   wizardTrain.addPassenger(wizard3);
   Wizard* wizard4 = new Wizard("Neal", 88, 73);
   wizardTrain.addPassenger(wizard4);

   //Print all the Wizard Train passengers
   cout << "\n\nHere are the wizards who have boarded the " << wizardTrain.getTrainName() << "!\n\n";
   wizardTrain.printPassengers();

   cout << endl << endl;

   //Create a Cat Train object
   Train<Cat*> kittyTrain("Hello Kitty Express", "Japan", 10);

   //Create Cats & then add them to the Cat Train
   Cat* cat1 = new Cat("Yella Cat", "mixed", "orange & yellow", 8);
   kittyTrain.addPassenger(cat1);
   Cat* cat2 = new Cat("Mouth", "mixed", "black & white", 8);
   kittyTrain.addPassenger(cat2);
   Cat* cat3 = new Cat("Chloe", "siamese", "black & white", 3);
   kittyTrain.addPassenger(cat3);
   Cat* cat4 = new Cat("Brutus", "hairless", "skin color", 4);
   kittyTrain.addPassenger(cat4);
   Cat* cat5 = new Cat("Wally", "catdog", "lime", 1);
   kittyTrain.addPassenger(cat5);
   Cat* cat6 = new Cat("Gladass", "siamese", "black & white", 6);
   kittyTrain.addPassenger(cat6);

   //Print all the Cat Train passengers
   cout << "Here are the cats who have boarded the " << kittyTrain.getTrainName() << "!\n\n";
   kittyTrain.printPassengers();

   cout << endl << endl;
   return 0;
}

let me know if you have any doubts or problem running the program. thank you.

Add a comment
Know the answer?
Add Answer to:
C++ ONLY Please TRAINS DESCRIPTION You are completing a program that allows for several different types...
Your Answer:

Post as a guest

Your Name:

What's your source?

Earn Coins

Coins can be redeemed for fabulous gifts.

Not the answer you're looking for? Ask your own homework help question. Our experts will answer your question WITHIN MINUTES for Free.
Similar Homework Help Questions
  • C++ programming question, please help! Thank you so much in advance!!! In this exercise, you will...

    C++ programming question, please help! Thank you so much in advance!!! In this exercise, you will work with 2 classes to be used in a RPG videogame. The first class is the class Character. The Character class has two string type properties: name and race. The Character class also has the following methods: a constructor Character(string Name, string Race), that will set the values for the name and the race variables set/get functions for the two attributes a function print(),...

  • Language = C++ How to complete this code? C++ Objects, Structs and Linked Lists. Program Design:...

    Language = C++ How to complete this code? C++ Objects, Structs and Linked Lists. Program Design: You will create a class and then use the provided test program to make sure it works. This means that your class and methods must match the names used in the test program. Also, you need to break your class implementation into multiple files. You should have a car.h that defines the car class, a list.h, and a list.cpp. The link is a struct...

  • Project Description Complete the Film Database program described below. This program allows users to store a...

    Project Description Complete the Film Database program described below. This program allows users to store a list of their favorite films. To build this program, you will need to create two classes which are used in the program’s main function. The “Film” class will store information about a single movie or TV series. The “FilmCollection” class will store a set of films, and includes functions which allow the user to add films to the list and view its contents. IMPORTANT:...

  • Use C++! This program uses the class myStack to determine the highest GPA from a list of students with their GPA.The program also outputs the names of the students who received the highest GPA. Redo t...

    Use C++! This program uses the class myStack to determine the highest GPA from a list of students with their GPA.The program also outputs the names of the students who received the highest GPA. Redo this program so that it uses the STL list and STL queue! Thank you! HighestGPAData.txt* 3.4 Randy 3.2 Kathy 2.5 Colt 3.4 Tom 3.8 Ron 3.8 Mickey 3.6 Peter 3.5 Donald 3.8 Cindy 3.7 Dome 3.9 Andy 3.8 Fox 3.9 Minnie 2.7 Gilda 3.9 Vinay...

  • This is a c++ program. Use the description from Parking Ticket Simulator (listed below) as a basis, where you need to...

    This is a c++ program. Use the description from Parking Ticket Simulator (listed below) as a basis, where you need to create a Car class and Police Officer class, to create a new simulation. Write a simulation program (refer to the Bank Teller example listed below) that simulates cars entering a parking lot, paying for parking, and leaving the parking lot. The officer will randomly appear to survey the cars in the lot to ensure that no cars are parked...

  • java Object Oriented Programming The assignment can be done individually or in teams of two. Submit one as...

    java Object Oriented Programming The assignment can be done individually or in teams of two. Submit one assignment per team of two via Omnivox and NOT MIO.Assignments sent via MIO will be deducted marks. Assignments must be done alone or in groups and collaboration between individuals or groups is strictly forbidden. There will be a in class demo on June 1 make sure you are prepared, a doodle will be created to pick your timeslot. If you submit late, there...

  • using C++ language!! please help me out with this homework In this exercise, you will have...

    using C++ language!! please help me out with this homework In this exercise, you will have to create from scratch and utilize a class called Student1430. Student 1430 has 4 private member attributes: lastName (string) firstName (string) exams (an integer array of fixed size of 4) average (double) Student1430 also has the following member functions: 1. A default constructor, which sets firstName and lastName to empty strings, and all exams and average to 0. 2. A constructor with 3 parameters:...

  • C++ Object Oriented assignment Can you please check the program written below if it has appropriately...

    C++ Object Oriented assignment Can you please check the program written below if it has appropriately fulfilled the instructions provided below. Please do the necessary change that this program may need. I am expecting to get a full credit for this assignment so put your effort to correct and help the program have the most efficient algorithm within the scope of the instruction given. INSTRUCTIONS Create a fraction class and add your Name to the name fraction and use this...

  • This project is divided into 3 parts: Part 1. Create a new project and download the arrayList and...

    This project is divided into 3 parts: Part 1. Create a new project and download the arrayList and unorderedArrayList templates that are attached. Create a header file for your unorderedSet template and add it to the project. An implementation file will not be needed since the the new class will be a template. Override the definitions of insertAt, insertEnd, and replaceAt in the unorderedSet template definition. Implement the template member functions so that all they do is verify that the...

  • In this project, you are asked to design and implement a class for double link list...

    In this project, you are asked to design and implement a class for double link list to hold integers: – The class should be called DList, you also need a class called Node that contains an int as the data – Node needs the following data: int data; //the data held by the node Node* parent; //the parent of the Node Node* child; //the child of the Node – Node needs the following public functions: Node(int)// constructor to create a...

ADVERTISEMENT
Free Homework Help App
Download From Google Play
Scan Your Homework
to Get Instant Free Answers
Need Online Homework Help?
Ask a Question
Get Answers For Free
Most questions answered within 3 hours.
ADVERTISEMENT
ADVERTISEMENT
ADVERTISEMENT