Question

In: Computer Science

Design a Ship class that has the following members: • A member variable for the name...

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 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.

After you have created these classes, create a driver program that defines an array pointers to your abstract base class Ship. It should present a menu to allow the user to enter a Cruise Ship or a Cargo Ship. When the user selects cruise ship, it should prompt them for the name, year built, and the number of passengers. When the user selects cargo ship, it should prompt them ffor the name, year built, and the maxium cargo load (in tons). Each selection should create an object that can be stored into your array of pointers. When the program exits, it should display the history of the session which includes each of the objects the user created which will be a combination of cruise ships and cargo ships. An example output is shown here:

Menu

1. Cruise Ship
2. Cargo Ship
3. Quit
Please make your selection: 2

Please enter the name of the ship: Big Bertha
Please enter the year the ship was built: 1975
Please enter the capacity (tons): 50000

Menu

1. Cruise Ship
2. Cargo Ship
3. Quit
Please make your selection: 1

Please enter the name of the ship: Disney Magic
Please enter the year the ship was built: 1998
Please enter the capacity (passengers): 2400

Menu

1. Cruise Ship
2. Cargo Ship
3. Quit
Please make your selection: 3

The history of your session is

Selected Cargo Ship

Name: Big Bertha
Year: 1975
Capacity (tons): 50000

Selected Cruise Ship

Name: Disney Magic
Year: 1998
Capacity (passengers): 2400

Solutions

Expert Solution

Ship.h

#ifndef SHIP_H
#define SHIP_H

//using namespace std;


class Ship
{
    public:
        Ship();
        std::string getName();
        void setName(std::string name);
        std::string getYearBuilt();
        void setYear(std::string yar);
        std::string printNameAndYear();
        ~Ship();
    private:
        std::string name;
        std::string year;
};

#endif // SHIP_H 

Ship.cpp

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


Ship::Ship()
{
    name = "";
    year = "";
}

std::string Ship::getName() {
    return name;
}

void Ship::setName(std::string nameIn) {
    name = nameIn;
}

std::string Ship::getYearBuilt() {
    return year;
}

void Ship::setYear(std::string yearIn) {
    //std::string yar;
    year = yearIn;
}

std::string Ship::printNameAndYear() {
    return "The " + name + " was built in " + year + "\n";
}

Ship::~Ship()
{
    //dtor
}

Main function:

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

using namespace std;

int main()
{
    Ship *santaMariaPtr = new Ship;
    santaMariaPtr->setName("Santa Maria");
    santaMariaPtr->setYear("1763");
    cout << santaMariaPtr->printNameAndYear();

    cout << "Press and key and return to exit ";
    char c;
    cin >> c;

    return 0;
}

CruiseShip.h

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

//using namespace std;


class CruiseShip : public Ship
{
    public:
        CruiseShip();
        ~CruiseShip();
    protected:
    private:
};

#endif // CRUISESHIP_H 

CruiseShip.cpp

#include "CruiseShip.h"
#include<string.h>
CruiseShip::CruiseShip()
{
    //ctor
}

CruiseShip::~CruiseShip()
{
    //dtor
}

Related Solutions

In c++, define a class with the name BankAccount and the following members: Data Members: accountBalance:...
In c++, define a class with the name BankAccount and the following members: Data Members: accountBalance: balance held in the account interestRate: annual interest rate. accountID: unique 3 digit account number assigned to each BankAccount object. Use a static data member to generate this unique account number for each BankAccount count: A static data member to track the count of the number of BankAccount objects created. Member Functions void withdraw(double amount): function which withdraws an amount from accountBalance void deposit(double...
This question demonstrates the use of inheritance and polymorphism. Design a Ship class that has the...
This question demonstrates the use of inheritance and polymorphism. Design a Ship class that has 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 overrides the toString method in the Object class. The Ship class toString method should display the ship’s name and the year it was built. Design...
/*Design and code a class Calculator that has the following * tow integer member variables, num1...
/*Design and code a class Calculator that has the following * tow integer member variables, num1 and num2. * - a method to display the sum of the two integers * - a method to display the product * - a method to display the difference * - a method to display the quotient * - a method to display the modulo (num1%num2) * - a method toString() to return num1 and num2 in a string * - Test your...
Write a class called Person that has two private data members - the person's name and...
Write a class called Person that has two private data members - the person's name and age. It should have an init method that takes two values and uses them to initialize the data members. It should have a get_age method. Write a separate function (not part of the Person class) called std_dev that takes as a parameter a list of Person objects and returns the standard deviation of all their ages (the population standard deviation that uses a denominator...
Program Specification Design an inventory class that stores the following members: serialNum: An integer that holds...
Program Specification Design an inventory class that stores the following members: serialNum: An integer that holds a part's serial number. manufactDate: A member that holds the date the part was manufactured. lotNum: An integer that holds the part's lot number. The class should have appropriate setter and getter functions. Next, design a stack class that can hold objects of the class described above. If you wish, you may use the linked list from program 5 as a basis for designing...
Design a class named Pet, which should have the following fields: Name – The name field...
Design a class named Pet, which should have the following fields: Name – The name field holds the name of a pet. Type – The type field holds the type of animal that is the pet. Example values are “Dog”, “Cat”, and “Bird”. Age – The age field holds the pet’s age. The Pet class should also have the following methods: setName – The setName method stores a value in the name field. setType – The setType method stores a...
I. Create the class Item with the following members: 1. id, a protected variable of type...
I. Create the class Item with the following members: 1. id, a protected variable of type int 2. name, a protected variable of type string 3. price, a protected variable of type double 4. a public non-default constructor which accepts three parameters to initialize the variables above 5. a copy constructor 6. overload the = operator such that both operands are of type Item 7. overload the = operator such that the right operand is of type double. Assign the...
2. Report Heading Design a class called Heading that has data members to hold the company...
2. Report Heading Design a class called Heading that has data members to hold the company name and the report name. A two-parameter default constructor should allow these to be specified at the time a new Heading object is created. If the user creates a Heading object without passing any arguments, “ABC Industries” should be used as a default value for the company name and “Report” should be used as a default for the report name. The class should have...
C++ make a rational class that includes these members for rational -private member variables to hold...
C++ make a rational class that includes these members for rational -private member variables to hold the numerator and denominator values -a default constructor -an overloaded constructor that accepts 2 values for an initial fraction -member fractions add(), sub(), mul(), div(), less(), eq(), and neq() (less should not return true if the object is less than argument) -a member function that accepts an argument of type of ostream that writes the fraction to that open output stream do not let...
C++ PersonData and CustomerData classes Design a class named PersonData with the following member variables: lastName...
C++ PersonData and CustomerData classes Design a class named PersonData with the following member variables: lastName firstName address city state zip phone Write the appropriate accessor and mutator functions for these member variables. Next, design a class named CustomerData, which is derived from the PersonData class. The CustomerData class should have the following member variables: customerNumber mailingList The customerNumber variable will be used to hold a unique integer for each customer. The mailingList variable should be a bool. It will...
ADVERTISEMENT
ADVERTISEMENT
ADVERTISEMENT