In: Computer Science
Design a Ship class that has the following members:
• A member variable for the name of the ship (a string)
• A member variable for the year that the ship was built (a
string)
• A constructor and appropriate accessors and mutators
• A virtual print function that displays the ship’s name and the
year it was built.
Design a CruiseShip class that is derived from the Ship class.
The CruiseShip class
should have the following members:
• A member variable for the maximum number of passengers (an int
)
• A constructor and appropriate accessors and mutators
• A print function that overrides the print function in the base
class. The CruiseShip class’s print function should display only
the ship’s name and the maximum number of passengers.
Design a CargoShip class that is derived from the Ship class.
The CargoShip class
should have the following members:
• A member variable for the cargo capacity in tonnage (an int
).
• A constructor and appropriate accessors and mutators.
• A print function that overrides the print function in the base
class. The CargoShip class’s print function should display only the
ship’s name and the ship’s cargo capacity.
After you have created these classes, create a driver program that defines an array pointers to your abstract base class Ship. It should present a menu to allow the user to enter a Cruise Ship or a Cargo Ship. When the user selects cruise ship, it should prompt them for the name, year built, and the number of passengers. When the user selects cargo ship, it should prompt them ffor the name, year built, and the maxium cargo load (in tons). Each selection should create an object that can be stored into your array of pointers. When the program exits, it should display the history of the session which includes each of the objects the user created which will be a combination of cruise ships and cargo ships. An example output is shown here:
Menu
1. Cruise Ship
2. Cargo Ship
3. Quit
Please make your selection: 2
Please enter the name of the ship: Big Bertha
Please enter the year the ship was built: 1975
Please enter the capacity (tons): 50000
Menu
1. Cruise Ship
2. Cargo Ship
3. Quit
Please make your selection: 1
Please enter the name of the ship: Disney Magic
Please enter the year the ship was built: 1998
Please enter the capacity (passengers): 2400
Menu
1. Cruise Ship
2. Cargo Ship
3. Quit
Please make your selection: 3
The history of your session is
Selected Cargo Ship
Name: Big Bertha
Year: 1975
Capacity (tons): 50000
Selected Cruise Ship
Name: Disney Magic
Year: 1998
Capacity (passengers): 2400
Ship.h
#ifndef SHIP_H #define SHIP_H //using namespace std; class Ship { public: Ship(); std::string getName(); void setName(std::string name); std::string getYearBuilt(); void setYear(std::string yar); std::string printNameAndYear(); ~Ship(); private: std::string name; std::string year; }; #endif // SHIP_H
Ship.cpp
#include <iostream> #include "Ship.h" Ship::Ship() { name = ""; year = ""; } std::string Ship::getName() { return name; } void Ship::setName(std::string nameIn) { name = nameIn; } std::string Ship::getYearBuilt() { return year; } void Ship::setYear(std::string yearIn) { //std::string yar; year = yearIn; } std::string Ship::printNameAndYear() { return "The " + name + " was built in " + year + "\n"; } Ship::~Ship() { //dtor }
Main function:
#include <iostream> #include "Ship.h" using namespace std; int main() { Ship *santaMariaPtr = new Ship; santaMariaPtr->setName("Santa Maria"); santaMariaPtr->setYear("1763"); cout << santaMariaPtr->printNameAndYear(); cout << "Press and key and return to exit "; char c; cin >> c; return 0; }
CruiseShip.h
#ifndef CRUISESHIP_H #define CRUISESHIP_H #include "Ship.h" //using namespace std; class CruiseShip : public Ship { public: CruiseShip(); ~CruiseShip(); protected: private: }; #endif // CRUISESHIP_H
CruiseShip.cpp
#include "CruiseShip.h" #include<string.h> CruiseShip::CruiseShip() { //ctor } CruiseShip::~CruiseShip() { //dtor }