Academic Integrity: tutoring, explanations, and feedback — we don’t complete graded work or submit on a student’s behalf.

Ship, CruiseShip, and CargoShip Classes. Program in C++. Must be split into Main

ID: 3706773 • Letter: S

Question

Ship, CruiseShip, and CargoShip Classes. Program in C++.

Must be split into Main.cpp, Ship.cpp, and Ship.h files.
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.

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. (See Program 15-14 , lines 17 through 22, for an
example of how to do this.) The program should then step through the array, calling
each object’s print function.

Explanation / Answer

C++ Programing for Ship, Cruiseship and Cargoship Classes:

//Headeres file section

#include<iostream>

#include<string>

using namespace std;

//Create a class name "Ship"

class Ship

{

//Declare member variables

private:

//A member variable for the name

//of the ship ( a string )

string shipName;

//A member variable for the year

// that the ship was built ( a string )

string shipBuilt;

public:

//A constructor

Ship(string name, string yearBuilt)

{

shipName = name;

shipBuilt=yearBuilt;

}

//accessors and mutators methods

string getname()

{

return shipName;

}

string getbuilt()

{

return shipBuilt;

}

//A virtual print function that displays

//the ship's name and the year it was built

virtual void print()

{

//Display output

cout<<"Ship's Name:"<<getname()<<" and Year built:"<<getbuilt()<<endl;

}

};

//Create a CruiseShip class that is derived

//from the Ship class.

class CruiseShip:public Ship

{

private:

//A member variable for the maximum

//number of passengers (an int)

int passengers;

public:

//A constructor and appropriate accessors and mutators

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 functionshould display

//only the ship's name and the maximum number of passengers.

virtual void print()

{//Display output

cout<<"Ship's Name: "<<getname()

<<" and Maximum number passengers:"<<passengers<<endl;

}

};

//Create a CargoShip class that is derived

//from the Ship class.

class CargoShip:public Ship

{

//Declare variables

private:

//A member variable for the cargo capacity

//in tonnage (an int)

int tonnage;

public:

//A constructor and appropriate accessors and mutators

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()

{

//Display output

cout<<"Ship's Name:"<<getname()<<" and capacity:"<<tonnage<<" tonnage"<<endl;

}

};

//main function

int main()

{

//Declare variable

int i;

/*Array elements should be intialized with

the addresses of dynamically allocated Ship,

CruiseShip, and CargoShip objects*/

Ship *ships[3]={new Ship("CruiseShip", "2011"),

new CruiseShip("Ship","2012",2000),

new CargoShip("Cargo","2013",4000)

};

//calling each object's print function.

for(i=0;i<3;i++)

ships[i]->print();

//Pause the system for a while

system("pause");

return 0;

}

Hire Me For All Your Tutoring Needs
Integrity-first tutoring: clear explanations, guidance, and feedback.
Drop an Email at
drjack9650@gmail.com
Chat Now And Get Quote