#include <iostream> #include <string> using namespace std; class Ship { private:
ID: 3557377 • Letter: #
Question
#include <iostream>
#include <string>
using namespace std;
class Ship
{
private:
string name;
string built;
public:
Ship(string n, string y)
{
name = n;
built = y;
}
string getname()
{
return name;
}
string getbuilt()
{
return built;
}
virtual void print()
{
cout<<"Ship Name : "<<getname()<< ", Year built: " << getbuilt() <<endl;
}
};
class CargoShip: public Ship //Cargoship is derived from the Ship class
{
private:
int tonnage;
public:
CargoShip(string n, string y, int t) : Ship(n,y)
{
tonnage = t;
}
virtual void print()
{
cout<<"Ship Name : "<< getname() << ", Maximum tonnage: " << tonnage << endl;
}
};
class CruiseShip: public Ship //Cruiseship is also derived from ship class
{
private:
int pass;
public:
CruiseShip(string n, string y, int p) : Ship(n,y)
{
pass=p;
}
virtual void print()
{
cout<<"Ship Name : "<< getname() << ", Maximum number passengers: " << pass << endl;
}
};
int main()
{
cout << "WELCOME TO SHIP INFORMATION!" << endl;
cout << "__________________________________________________" << endl;
Ship *ships[3]= {new Ship("Titanic", "1909"),
new CargoShip("Cargo", "2010", 50000),
new CruiseShip("Cruise Ship", "2014", 500)
};
int i;
for(i = 0; i < 3; i++)
ships[i]->print();
return 0;
}
Explanation / Answer
Ship class:
CargoShip class:
ShipDemo class:
Related Questions
Navigate
Integrity-first tutoring: explanations and feedback only — we do not complete graded work. Learn more.