C++ Project Name: IC25_Ships Design a Ship class that has the following members:
ID: 3766161 • Letter: C
Question
C++ Project Name: IC25_Ships
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 (an int)
• 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.
Explanation / Answer
Complete Program:
#include <iostream>
#include <string>
using namespace std;
class Ship
{
private:
string name;
int year;
public:
Ship(string sName, int sYear)
{
name = sName;
year = sYear;
}
string getName()
{
return name;
}
int getYear()
{
return year;
}
void setName(string sName)
{
name = sName;
}
void setYear(int sYear)
{
year = sYear;
}
virtual void print()
{
cout << "Ship's Name: " << getName() << endl;
cout << "Year Built: " << getYear() << endl;
}
};
class CruiseShip : public Ship
{
private:
int passengers;
public:
CruiseShip(string sName, int sYear, int crPassengers) : Ship(sName, sYear)
{
passengers = crPassengers;
}
void print()
{
cout << "Ship's Name: " << getName() << endl;
cout << "Year Built: " << getYear() << endl;
cout << "Passengers: " << passengers << endl;
}
};
class CargoShip : public Ship
{
private:
int capacity;
public:
CargoShip(string sName, int sYear, int caCapacity) : Ship(sName, sYear)
{
capacity = caCapacity;
}
void print()
{
cout << "Ship's Name: " << getName() << endl;
cout << "Year Built: " << getYear() << endl;
cout << "Capacity: " << capacity << endl;
}
};
int main()
{
Ship *ships[3] = {new Ship("Ship", 2012), new CruiseShip("CruiseShip", 2013, 2500), new CargoShip("CargoShip", 2014, 5000)};
for(int i = 0; i < 3; i++)
{
ships[i]->print();
cout << endl;
}
system("pause");
return 0;
}
Sample Output:
Related Questions
drjack9650@gmail.com
Navigate
Integrity-first tutoring: explanations and feedback only — we do not complete graded work. Learn more.