Tuesday, February 6, 2018

Programming Challenge 15.12 - Ship CruiseShip and CargoShip Classes

Example Files: ShipCruiseShipCargoShip.7z

Ship.h


#ifndef SHIP_H_
#define SHIP_H_

#include <string>

class Ship
{
    protected:
        std::string shipName;                    // The ship name
        std::string yearConstructed;            // Year the ship was built

    public:
        // Constructor
        Ship()
        {
            shipName = " ";
            yearConstructed = " ";
        }

        // Parameterized constructor
        Ship(std::string, std::string);

        // Mutator functions
        void set(std::string, std::string);

        // Accessor function
        std::string getShipName() const
        { return shipName; }

        std::string getYearConstructed() const
        { return yearConstructed; }

        virtual void print() const;
};

#endif

Ship.cpp


#include "Ship.h"

#include <iostream>
using std::cout;


/* **********************************************************
            Ship::Ship() - Construtor
   ********************************************************** */

Ship::Ship(std::string name, std::string built)
{
    set(name, built);
}

/* **********************************************************
            Ship::set() : string, string
    This function stores the arguments passed to it in the
    shipName and yearConstructed variables.
   ********************************************************** */

void Ship::set(std::string name, std::string built)
{
    shipName = name;
    yearConstructed = built;
}

/* **********************************************************
            v Ship::print()
    This function outputs a ship's name and the year it was
    built.
   ********************************************************** */

void Ship::print() const
{
    cout << "Ship Name:  " << getShipName() << "\n";
    cout << "Year Built: " << getYearConstructed();
}

CruiseShip.h


#ifndef CRUISE_SHIP_H_
#define CRUISE_SHIP_H_

#include "Ship.h"

// CruiseShip class derived from the Ship class
class CruiseShip : public Ship
{
    private:
        int maxPassengers;            // Holding the maximum number of passengers

    public:
        // Constructor
        CruiseShip() : Ship()
        {
            maxPassengers = 0;
        }

        // Parameterized Constructor
        CruiseShip(std::string, std::string, int);

        // Mutator functions
        void set(int);

        // Accessor functions
        std::string getShipName() const
        { return shipName; }

        int getMaxPassengers() const
        { return maxPassengers; }

        virtual void print() const;
};

#endif

CruiseShip.cpp


#include "CruiseShip.h"

#include <iostream>
using std::cout;

/* **********************************************************
            CruiseShip::CruiseShip() - Constructor
   ********************************************************** */

CruiseShip::CruiseShip(std::string name, std::string built, int maxPassengers) :
                        Ship(name, built)
{
    set(maxPassengers);
}

/* **********************************************************
            CruiseShip::set() : int
    This function stores the argument passed to it in the
    maxPassengers variable.
   ********************************************************** */

void CruiseShip::set(int maxNum)
{
    maxPassengers = maxNum;
}

/* **********************************************************
            v CruiseShip::print() : overridden function
    This function outputs the ship's name and maximum number
    of passengers.
   ********************************************************** */

void CruiseShip::print() const
{
    cout << "Ship Name: "                    << getShipName() << "\n";
    cout << "Max. Passenger Capacity: " << getMaxPassengers();
}

CargoShip.h


#ifndef CARGO_SHIP_H_
#define CARGO_SHIP_H_

#include "Ship.h"

// CargoShip class derived from the Ship class
class CargoShip : public Ship
{
    private:
        int cargoCapacity;            // Holding the cargo capacity in tonnage

    public:
        // Constructor
        CargoShip() : Ship()
        {
            cargoCapacity = 0;
        }

        // Parameterized Constructor
        CargoShip(std::string, std::string, int);

        // Mutator function
        void set(int);

        // Accessor function
        int getCargoCapacity() const
        { return cargoCapacity; }

        virtual void print() const;
};

#endif

CargoShip.cpp


#include "CargoShip.h"

#include <iostream>
using std::cout;

/* **********************************************************
            CargoShip::CargoShip() - Constructor
   ********************************************************** */

CargoShip::CargoShip(std::string name, std::string built, int capacity) :
                     Ship(name, built)
{
    set(capacity);
}

/* **********************************************************
            CargoShip::set() : int
    This function stores the argument passed to it in the
    cargoCapacity variable.
   ********************************************************** */

void CargoShip::set(int capacity)
{
    cargoCapacity = capacity;
}

/* **********************************************************
            v CargoShip::print() : overridden function
    This function outputs a ship's name and cargo capacity in
    tonnage.
   ********************************************************** */

void CargoShip::print() const
{
    cout << "Ship Name: " << getShipName() << "\n";
    cout << "Cargo Capacity (t): " << getCargoCapacity();
}

ShipClassesDm.cpp


#include <iostream>
using std::cin;
using std::cout;

int main()
{
    // An array of pointers to ship objects
    array<Ship *, 3> ships = { new Ship("TS Lucitania", "1925"),
                                        new CruiseShip("MS Oriana", "1975", 560),
                                        new CargoShip("CS Al Dahna", "2016", 195636)
                                     };

    cout << "INTERNATIONAL SHIP DATABASE\n\n";

    // Output the ships
    for (auto shipTypes : ships)
    {
        shipTypes->print();
        cout << "\n\n";
    }

    cout << "Thank you for using our database!";

    cin.get();
    cin.ignore();
    return 0;
}

Example Output:



No comments:

Post a Comment