Design a ship class that has the following members A member

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\'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 address of dynamically allocated ship, Cruiseship, and Cargoship. (see Program 15.14, line 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.

Solution

Here are the three classes for you:

#include <iostream>
using namespace std;
class Ship       //Base class.
{
protected:
string name;
string year;
public:
Ship(string n, string y)
{
name = n;
year = y;
}
string getName()
{
return name;
}
string getYear()
{
return year;
}
void setName(string n)
{
name = n;
}
void setYear(string y)
{
year = y;
}
virtual void print()       //Virtual class.
{
cout<<\"Ship name: \"<<name<<endl;
cout<<\"Year built: \"<<year<<endl;
}
};

class CruiseShip : Ship
{
int maxPassengers;
public:
CruiseShip(string n, string y, int m) : Ship(n, y)
{
maxPassengers = m;
}
void setMaxPassengers(int m)
{
maxPassengers = m;
}
int getMaxPassengers()
{
return maxPassengers;
}
void print()
{
cout<<\"Ship name: \"<<name<<endl;
cout<<\"Max Passengers: \"<<maxPassengers<<endl;
}
};

class CargoShip : Ship
{
int tonnage;
public:
CargoShip(string n, string y, int t) : Ship(n, y)
{
tonnage = t;
}
void setTonnage(int t)
{
tonnage = t;
}
int getTonnage()
{
return tonnage;
}
void print()
{
cout<<\"Ship name: \"<<name<<endl;
cout<<\"Cargo capacity: \"<<tonnage<<endl;
}
};
  
To demonstrate, you\'re referring to some other program it seems, which you didn\'t gave a copy here.

 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 buil
 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 buil

Get Help Now

Submit a Take Down Notice

Tutor
Tutor: Dr Jack
Most rated tutor on our site