Academic Integrity: tutoring, explanations, and feedback — we don’t complete graded work or submit on a student’s behalf.

To develop in C++ Instrucciones: .Defina la clase Ship, Cruise Ship, y CargoShip

ID: 3735848 • Letter: T

Question

To develop in C++

Instrucciones: .Defina la clase Ship, Cruise Ship, y CargoShip Implemente los operadores, Implemente sus constructores y destructor Construya una clase Ship que forme el arreglo con objetos tipo Cruise Ship y CargoShip. Ship, 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 Cruise Ship 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 Demonstrate the classes in a program that has an array of Ship. The array elements should be initialized with the addresses of dynamically allocated Ship,CruiseShip, and CargoShip objects The program should then step through the array, calling each object's print function.

Explanation / Answer

#include <iostream>
using namespace std;

class Ship // base class
{
private:
string name;
string yearBuilt;

public:
Ship(string name,string yearBuilt)//constructor
{
  this->name = name;
  this->yearBuilt = yearBuilt;
}
//set and get functions
void setName(string name)
{
  this->name = name;
}
string getName()
{
  return name;
}
void setYearBuilt(string yearBuilt)
{
  this->yearBuilt = yearBuilt;
}
string getYearBuilt()
{
  return yearBuilt;
}
virtual void print()// virtual function
{
  cout<<" Name of ship : "<<name<<" year built : "<<yearBuilt;
}
};

class CruiseShip : public Ship   // derived class
{
private:
int maxPassengers;

public:
CruiseShip(string name,string yearBuilt,int maxPassengers): Ship(name,yearBuilt)//passing arguments to base class constructor
{
this->maxPassengers = maxPassengers;
}
void setMaxPassengers(int maxPassengers)
{
this->maxPassengers = maxPassengers;
}
int getMaxPassengers()
{
return maxPassengers;
}
void print()
{
cout<<" Name of Ship : "<<getName()<<" Maximum Passengers in cruise : "<<maxPassengers;
}

};

class CargoShip:public Ship
{
private:
int capacity;

public:
CargoShip(string name,string yearBuilt,int capacity):Ship(name,yearBuilt)//passing arguments to base class constructor
{
this->capacity = capacity;
}
void setCapacity(int capacity)
{
this->capacity = capacity;
}
int getCapacity()
{
return capacity;
}
void print()
{
cout<<" Name of Ship : "<<getName()<<" Cargo Capacity : "<<capacity;
}
};
int main() {

Ship *ship[5]; // array of pointers of base class

int i;

// creating objects of derived classed using base class pointers
ship[0] = new Ship("HMCS Columbia","1985");
ship[1] = new CargoShip("ThunderBay","1956",200);
ship[2] = new CruiseShip("Adonia","1983",450);
ship[3] = new CruiseShip("fathom","1990",600);
ship[4] = new CargoShip("liberty Ship","1981",400);

for(i=0;i<5;i++)
{
  ship[i]->print();// calling print function
}
return 0;
}

Output:

Name of ship : HMCS Columbia year built : 1985
Name of Ship : ThunderBay Cargo Capacity : 200
Name of Ship : Adonia Maximum Passengers in cruise : 450
Name of Ship : fathom Maximum Passengers in cruise : 600
Name of Ship : liberty Ship Cargo Capacity : 400

Do ask if any query. Please upvote.

Hire Me For All Your Tutoring Needs
Integrity-first tutoring: clear explanations, guidance, and feedback.
Drop an Email at
drjack9650@gmail.com
Chat Now And Get Quote