Answered step by step
Verified Expert Solution
Link Copied!

Question

1 Approved Answer

C++ in this code i have Implement 3 classes Ship and Passenger_Ship and Cargo_Ship , i need a cpp and h file for each class

C++ in this code i have Implement 3 classes Ship and Passenger_Ship and Cargo_Ship , i need a cpp and h file for each class , but all the classes at the same main.cpp

ASAP PLEASE >>>

/ C++ program to create classes showing inheritance relationship

#include

#include

#include

using namespace std;

// base class

class Ship

{

private:

string model;

string captain;

int year;

int maxSpeed;

public:

Ship(string model, string captain, int year, int maxSpeed);

void setModel(string model);

void setCaptain(string captain);

void setYear(int year);

void setMaxSpeed(int maxSpeed);

string getModel() const;

string getCaptain() const;

int getYear() const;

int getMaxSpeed() const;

virtual void printDetails() const;

};

// parameterized constructor

Ship::Ship(string model, string captain, int year, int maxSpeed)

{

this->model = model;

this->captain = captain;

if(year > 0) // validate year to be non-negative

this->year = year;

else

this->year = 1900; // default year

if(maxSpeed >= 0) // validate maxSpeed to be non-negative

this->maxSpeed = maxSpeed;

else

this->maxSpeed = 0; // default max speed

}

// setters

void Ship:: setModel(string model)

{

this->model = model;

}

void Ship:: setCaptain(string captain)

{

this->captain = captain;

}

void Ship:: setYear(int year)

{

if(year > 0)

this->year = year;

}

void Ship:: setMaxSpeed(int maxSpeed)

{

if(maxSpeed >= 0)

this->maxSpeed = maxSpeed;

}

// getters

string Ship:: getModel() const

{

return model;

}

string Ship:: getCaptain() const

{

return captain;

}

int Ship:: getYear() const

{

return year;

}

int Ship:: getMaxSpeed() const

{

return maxSpeed;

}

// display details of ship

void Ship:: printDetails() const

{

cout<<"Model: "<

cout<<"Captain: "<

cout<<"Year : "<

cout<<"Max Speed: "<

}

// Derived class

class Passenger_Ship : public Ship

{

private:

int seatCost;

int numSeats;

float MPG;

public:

Passenger_Ship(string model, string captain, int year, int maxSpeed, int seatCost, int numSeats, float MPG);

void setSeatCost(int seatCost);

void setNumSeats(int numSeats);

void setMPG(float MPG);

int getSeatCost() const;

int getNumSeats() const;

float getMPG() const;

void printDetails() const;

friend bool existsInYear(const Passenger_Ship &ship, int year);

};

// parameterized constructor

Passenger_Ship::Passenger_Ship(string model, string captain, int year, int maxSpeed, int seatCost, int numSeats, float MPG)

: Ship(model, captain, year, maxSpeed)

{

if(seatCost >= 0) // validate seatCost to be non-negative

{

this->seatCost = seatCost;

}else

this->seatCost = 0; // default value

if(numSeats >= 0) // validate numSeats to be non-negative

this->numSeats = numSeats;

else

this->numSeats = 0 ; // default value

if(MPG >= 0) // validate MPG to be non-negative

this->MPG = MPG;

else

this->MPG = 0; // default value

}

// setters

void Passenger_Ship:: setSeatCost(int seatCost)

{

if(seatCost >= 0)

this->seatCost = seatCost;

}

void Passenger_Ship:: setNumSeats(int numSeats)

{

if(numSeats >= 0)

this->numSeats = numSeats;

}

void Passenger_Ship:: setMPG(float MPG)

{

if(MPG >= 0)

this->MPG = MPG;

}

// getters

int Passenger_Ship:: getSeatCost() const

{

return seatCost;

}

int Passenger_Ship:: getNumSeats() const

{

return numSeats;

}

float Passenger_Ship:: getMPG() const

{

return MPG;

}

// display details of passenger ship

void Passenger_Ship:: printDetails() const

{

Ship::printDetails(); // call printDetails of ship class

cout<<"Seat cost: $"<

cout<<"Number of seats: "<

cout<<"Miles per gallon(MPG): "<

}

// friend function that returns true if ship exist in year else false

bool existsInYear(const Passenger_Ship &ship, int year)

{

return(ship.getYear() == year);

}

// Derived class

class Cargo_Ship: public Ship

{

private:

int containerCost;

int capacity;

string fuelType;

public:

Cargo_Ship(string model, string captain, int year, int maxSpeed, int containerCost, int capacity, string fuelType);

void setContainerCost(int containerCost);

void setCapacity(int capacity);

void setFuelType(string fuelType);

int getContainerCost() const;

int getCapacity() const;

string getFuelType() const;

void printDetails() const;

};

// parameterized constructor

Cargo_Ship::Cargo_Ship(string model, string captain, int year, int maxSpeed, int containerCost, int capacity, string fuelType)

: Ship(model, captain, year, maxSpeed)

{

if(containerCost >= 0) // validate containerCost to be non-negative

this->containerCost = containerCost;

else

this->containerCost = 0; // default value

if(capacity >= 0) // validate capacity to be non-negative

this->capacity = capacity;

else

this->capacity = 0; // default value

this->fuelType = fuelType;

}

// setters

void Cargo_Ship:: setContainerCost(int containerCost)

{

if(containerCost >= 0)

this->containerCost = containerCost;

}

void Cargo_Ship:: setCapacity(int capacity)

{

if(capacity >=0 )

this->capacity = capacity ;

}

void Cargo_Ship:: setFuelType(string fuelType)

{

this->fuelType = fuelType;

}

// getters

int Cargo_Ship:: getContainerCost() const

{

return containerCost;

}

int Cargo_Ship:: getCapacity() const

{

return capacity;

}

string Cargo_Ship:: getFuelType() const

{

return fuelType;

}

// display details of Cargo ship

void Cargo_Ship:: printDetails() const

{

Ship::printDetails(); // call Ship's printDetails function

cout<<"Container cost: $"<

cout<<"Capacity: "<

cout<<"Fuel Type: "<

}

int main()

{

// create objects of Passenger_Ship and Cargo_Ship

Passenger_Ship p1("Passenger Model 1","Passenger Captain 1", 1985, 320, 45, 120, 15.75);

Passenger_Ship p2("Passenger Model 2","Passenger Captain 2", 2002, 430, 25, 250, 20);

Cargo_Ship c1("Cargo Model 1","Cargo Captain 1", 2000, 70, 20, 200, "Diesel");

Cargo_Ship c2("Cargo Model 2","Cargo Captain 2", 2005, 120, 30, 275, "Petrol");

// display details

cout<<"P1:"<

p1.printDetails();

cout<<"P2:"<

p2.printDetails();

cout<<"C1:"<

c1.printDetails();

cout<<"C2:"<

c2.printDetails();

// test the existInYear function

cout<<"Passenger ship P1 exists in year 2002: "<

cout<<"Passenger ship P2 exists in year 2002: "<

return 0;

}

//end of program

Step by Step Solution

There are 3 Steps involved in it

Step: 1

blur-text-image

Get Instant Access to Expert-Tailored Solutions

See step-by-step solutions with expert insights and AI powered tools for academic success

Step: 2

blur-text-image

Step: 3

blur-text-image

Ace Your Homework with AI

Get the answers you need in no time with our AI-driven, step-by-step assistance

Get Started

Recommended Textbook for

SQL Server T-SQL Recipes

Authors: David Dye, Jason Brimhall

4th Edition

1484200616, 9781484200612

More Books

Students also viewed these Databases questions

Question

find all matrices A (a) A = 13 (b) A + A = 213

Answered: 1 week ago

Question

What is Change Control and how does it operate?

Answered: 1 week ago

Question

How do Data Requirements relate to Functional Requirements?

Answered: 1 week ago