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

Design a Ship class that has the following members: A member variable for the na

ID: 3691564 • Letter: D

Question

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 Six overloaded relational operators that compare 2 ships. Compare based on the year the ship was built. 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. Use the following code in your main function to test your classes: // Create an array of Ship pointers, initialized with // the addresses of some dynamically allocated objects. const int SIZE = 3; Ship *ships[SIZE] = { new Ship("Lolipop", 1960), new CruiseShip("Disney Magic", 1998, 2400), new CargoShip("Black Pearl", 1800, 50000) }; int firstShip = 0; int lastShip = 0; // Call each object's print function and check for newest and oldest. for (int index=0; index < SIZE; index++) { ships[index]->print(); cout << "---------------------------- "; //Use overloaded relational operators if(*ships[index] < *ships[firstShip]) firstShip = index; if(*ships[index] > *ships[lastShip]) lastShip = index; } cout<getYearBuilt() << endl; ships[firstShip]->print(); cout<<"The newest ship built was in " << ships[lastShip]->getYearBuilt() << endl; ships[lastShip]->print(); //free up the memory for (int index=0; index < SIZE; index++) { delete ships[index]; }

Explanation / Answer


CargoShip.cpp

#include <iostream>
#include "CargoShip.h"

using namespace std;

// constructor
CargoShip::CargoShip( const string &n, int t )
: Ship( n )
{
    setTonnage( t );
} // end CommissionEmployee constructor

// set base salary
void CargoShip::setTonnage( int t )
{
    tonnage = t;
} // end function setBaseSalary

// return base salary
int CargoShip::getTonnage() const
{
   return tonnage;
} // end function getBaseSalary

// print CommissionEmployee object
void CargoShip::print() const
{
    cout << "Name: " << name << endl;
    cout << "Cargo Capacity: " << tonnage << endl;
    cout << "--------------------------" << endl;
} // end function print
CargoShip::~CargoShip() {
}

CargoShip.h

#ifndef CARGOSHIP_H
#define   CARGOSHIP_H
#include "Ship.h"

class CargoShip : public Ship{
public:
   CargoShip( const std::string &, int = 0 );

   void setTonnage( int ); // set base salary
   int getTonnage() const; // return base salary

   virtual void print() const; // print CommissionEmployee object
   virtual ~CargoShip();
protected:
   int tonnage;
};

#endif   /* CARGOSHIP_H */

CruiseShip.cpp

#include <iostream>
#include "CruiseShip.h"

using namespace std;

// constructor
CruiseShip::CruiseShip( const string &n, int p )
: Ship( n )
{
    setPassengers( p );
} // end CommissionEmployee constructor

// set base salary
void CruiseShip::setPassengers( int p )
{
    passengers = p;
} // end function setBaseSalary

// return base salary
int CruiseShip::getPassengers() const
{
   return passengers;
} // end function getBaseSalary

// print CommissionEmployee object
void CruiseShip::print() const
{
    cout << "Name: " << name << endl;
    cout << "Maximum Passengers: " << passengers << endl;
    cout << "--------------------------" << endl;
} // end function print

CruiseShip::~CruiseShip() {
}

CruiseShip.h

#ifndef CRUISESHIP_H
#define   CRUISESHIP_H
#include "Ship.h"

class CruiseShip : public Ship{
public:
   CruiseShip( const std::string &, int = 0 );

   void setPassengers( int ); // set base salary
   int getPassengers() const; // return base salary

   virtual void print() const; // print CommissionEmployee object
   virtual ~CruiseShip();
protected:
   int passengers;
};

#endif   /* CRUISESHIP_H */

Ship.cpp
#include <iostream>
#include "Ship.h"

using namespace std;

Ship::Ship()
{
    name = year = "";
}
// constructor
Ship::Ship( const string &n, const string &y )
{
    name = n;
    year = y;
} // end CommissionEmployee constructor

Ship::Ship( const string &n )
{
    name = n;
} // end CommissionEmployee constructor

// set first name
void Ship::setName( const string &n )
{
   name = n; // should validate
} // end function setFirstName

// return first name
string Ship::getName() const
{
   return name;
} // end function getFirstName

// set gross sales amount
void Ship::setYear( const string &y )
{
    year = y;
} // end function setGrossSales

// return gross sales amount
string Ship::getYear() const
{
   return year;
} // end function getGrossSales

// print CommissionEmployee object
void Ship::print() const
{
    cout << "--------------------------" << endl;
    cout << "Name: " << name << endl;
    cout << "Year built: " << year << endl;
    cout << "--------------------------" << endl;
} // end function print

Ship::~Ship() {
}


Ship.h
#ifndef SHIP_H
#define   SHIP_H

class Ship {
public:

    Ship(); //Constructor
    Ship( const std::string &);
    Ship( const std::string &, const std::string & );

    void setName( const std::string & ); // set first name
    std::string getName() const; // return first name

    void setYear( const std::string & ); // set commission rate (percentage)
    std::string getYear() const; // return commission rate

    virtual void print() const; // print CommissionEmployee object
    virtual ~Ship();
protected:
    std::string name;
    std::string year; // commission percentage
};

#endif   /* SHIP_H */

main.cpp
#include <iostream>
#include <string>
#include "Ship.h"
#include "CruiseShip.h"
#include "CargoShip.h"

using namespace std;

int main()
{

    unsigned int array_size = 3;
  
    Ship *s [ array_size ] =
    {
        new Ship("Lolipop", "1960"),
        new CruiseShip("Disney Magic", 2400),
        new CargoShip("Black Pearl", 50000)
    };

    for(int i; i<3; i++)
    {
        s[i] -> print();
    }
}


sample output

Name: Lolipop                                                                                                                                               
Year built: 1960                                                                                                                                            
--------------------------                                                                                                                                  
Name: Disney Magic                                                                                                                                          
Maximum Passengers: 2400                                                                                                                                    
--------------------------                                                                                                                                  
Name: Black Pearl                                                                                                                                           
Cargo Capacity: 50000                                                                                                                                       
--------------------------