Ship, CruiseShip, and CargoShip Classes (in C++ language i use visual studios to code with)
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 contsructor and appropriate accessors and mutators
- A virtual print function that displays the ship's name and the year it was built (nobody seems to get this part right)
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 contsructor 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 contsructor 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.
Demonstrate the classes in a program that has an array of Ship pointers. The array elements should be initialized with the addresses of dynamically allocated Ship, CruiseShip, and CargoShip objects. The program should then step through the array, calling each object's print function.
#include "stdafx.h"//For use Visual C++ IDE //Header file section #include #include using namespace std;
//Create a Ship class class Ship { private: //A member variable for the name of the ship (a string) string name; //A member variable for the year that the ship was built (a string) string built; public: // Contsructor Ship(string n, string b) { name = n; built = b; } //accessors and mutators methods string getName() { return name; } string getBuilt() { return built; } //A virtual print function that displays //the ship's name and the year it was built virtual void print() { cout<<"Name:"<
class CruiseShip:public Ship { private: //A member variable for the maximum //number of passengers (an int) int passengers; public: // contsructor CruiseShip(string n, string y, int p) : Ship(n,y) { passengers=p; } //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. virtual void print() { cout<<"Name: "<
class CargoShip:public Ship { private: //A member variable for the cargo capacity in tonnage (an int) int tonnage; public: //Constructor CargoShip(string n, string y, int t) : Ship(n,y) { tonnage =t; } //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. virtual void print() { cout<<"Name:"<
int main() { int i; //An array of Ship pointers Ship *ships[3]={new Ship("Lolipop", "1960"), new CruiseShip("Disney Magic","2010",2400), new CargoShip("Black Pearl","2003",50000) }; //Display output for(i=0;i<3;i++) ships[i]->print();
//Pause the system for a while system("pause"); return 0; }