Question: in this code c++ i need 3 parts or files etc , Main.cpp and ship.cpp and ship.h only , ASAP please . / C++ program
in this code c++ i need 3 parts or files etc , Main.cpp and ship.cpp and ship.h only , ASAP please .
/ C++ program to create classes showing inheritance relationship #include
// 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: "< // 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: $"< // 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: $"< 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:"< cout<<"P2:"< // test the existInYear function cout<<"Passenger ship P1 exists in year 2002: "<
Step by Step Solution
There are 3 Steps involved in it
Get step-by-step solutions from verified subject matter experts
