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

I am needing assistance with Programming Challenge number 12 in Chapter 15. I fo

ID: 3773967 • Letter: I

Question

I am needing assistance with Programming Challenge number 12 in Chapter 15. I forgot to mention this needs to be in C++. It is as follows:

Design a Ship class that the following members:
• A field for the name of the ship ( a string).
• A field for the year that the ship was built ( a string).
• A constructor and appropriate accessors and mutators.
• A toString method that displays the ship’s name and the year it was built.

Design a CruiseShip class that extends the Ship class. The CruiseShip class should have the following members:
• A field for the maximum number of passengers ( an int).
• A constructor and appropriate accessors and mutators.
• A toString method that overrides the toString method in the base class. The CruiseShip class’s toString method should display only the ship’s name and the maximum number of passengers.

Design a CargoShip class that extends the Ship class. The CargoShip class should have the following members:
• A field for the cargo capacity in tonnage ( an int).
• A constructor and appropriate accessors and mutators.
• A toString method that overrides the toString method in the base class. The CargoShip class’s toString method should display only the ship’s name and the ship’s cargo capacity.

Demonstrate the classes in a program that has a Ship array or arrayList. Assign various Ship, CruiseShip, and CargoShip objects to the array elements. The program should then step through the array, calling each object’s toString method.

Explanation / Answer

ANS:

#include<iostream>
using namespace std;
class Ship
{
private:char name[50];
   char year[50];
public: Ship(){
   cout<<"enter Ship Name";cin>>name;
   cout<<"enter Built in Year";cin>>year;
   }
   void setName(char name[])
   {
   strcpy(this->name,name);
   }
   void setYear(char year[])
   {
   strcpy(this->year,year);
   }
   char* getName(){
   return name;
   }
   char* getYear(){
   return year;
   }
   void toString(){
cout<<"Ship Name: "<<name<<",Built in Year="<<year<<endl;
   }
};
//CruiseShip
class CruiseShip: public Ship
{
private:int numberOfPassengers;
public: CruiseShip(){
cout<<"Enter Maximum Number Of Passengers";cin>>numberOfPassengers;
}
   void setNumberOfPassengers(int n){
   numberOfPassengers=n;
   }
   int getNumberOfPassengers(){
   return numberOfPassengers;
   }
   void toString(){//override
cout<<"CruiseShip Name: "<<getName()<<",Maximum Number Of Passengers="<<getNumberOfPassengers()<<endl;
   }
};
//Cargoship class
class CargoShip: public Ship
{
private:int capacity;
public: CargoShip(){
cout<<"enter capacity in tonnes";cin>>capacity;
}
   void setCapacity(int n){
   capacity=n;
   }
   int getCapacity(){
   return capacity;
   }
   void toString(){//Override
cout<<"CargoShip Name: "<<getName()<<",Cargo Capacity in Tonnes="<<getCapacity()<<endl;
   }
};
int main()
{
Ship *p[3];//ship array
Ship q;//ship object
CruiseShip r;//CruiseShip object
CargoShip s;//CargoShip Object
p[0]=&q;//assigning ship object to Ship array
p[1]=&r;//assigning CruiseShip object to Ship array
p[2]=&s;//assigning CargoShip object to Ship array
for(int i=0;i<3;i++)
{
p[i]->toString();//call each object's toString()
}
}