Required language C Late submissions not accepted Skip Cruis
Solution
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...



