Required language: C++ Late submissions not accepted. Skip. Cruiseship, and Carg
ID: 3926129 • Letter: R
Question
Required language: C++ Late submissions not accepted. Skip. Cruiseship, and Cargoship Classes 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 disc. The Cruiseship class's print function should display only the ships 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.Explanation / Answer
Answer: run the code in IDE:
C++ Code
#include<iostream>
#include<string>
using namespace std;
class Ship //Create a class name "Ship"
{
private:
string shipName; //Declare member variables
string shipBuilt; //Declare member variables
public:
Ship(string name, string yearBuilt) //A constructor
{
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()
{
cout<<"Ship's Name:"<<getname()<<" and Year built:"<<getbuilt()<<endl; //Display output
}
};
//Create a CruiseShip class that is derived
//from the Ship class.
class CruiseShip:public Ship
{
private:
int passengers;
public:
CruiseShip(string n, string y, int p) : Ship(n,y) //A constructor and appropriate accessors and mutators
{
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()
{
cout<<"Ship's Name: "<<getname() //Display output
<<" and Maximum number passengers:"<<passengers<<endl;
}
};
//Create a CargoShip class that is derived
//from the Ship class.
class CargoShip:public Ship
{
private:
int tonnage;
public:
CargoShip(string n, string y, int t) : Ship(n,y) //A constructor and appropriate accessors and mutators
{
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<<"Ship's Name:"<<getname()<<" and capacity:"<<tonnage<<" tonnage"<<endl; //Display output
}
};
//main function
int main()
{
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;
}
Sample Output:
Ship's Name:CruiseShip and Year built:2011
Ship's Name: Ship and Maximum number passengers:2000
Ship's Name:Cargo and capacity:4000 tonnage...
Please rate me...
Related Questions
drjack9650@gmail.com
Navigate
Integrity-first tutoring: explanations and feedback only — we do not complete graded work. Learn more.