Help needed on C++ program:
Program Description:
Complete the Ship, CruiseShip, and CargoShip program (#12 in the 9th edition of the text).
Read the specific method requirements in the text.
Specific Requirements:
• Create the Ship class.
• Create CruiseShip and CargoShip classes that are derived from Ship.
• Create a small tester cpp file that has an array of Ship pointers (one each of Ship,
CruiseShip, and CargoShip). The program steps through the array, calling each object’s print method.
• No user input is required for this program.
Example output:
Name: Lollipop
Year built: 1960
----------------------------
Name: Disney Magic
Maximum passengers: 2400
----------------------------
Name: Black Pearl
Cargo capacity: 50000 tons
----------------------------
Press any key to continue . . .
Screenshot
-----------------------------------------------------------------------
Program
Ship.h
#pragma once
#include<iostream>
#include<string>
using namespace std;
//Create a class Ship
class Ship {
//Attributes
private:
string shipName;
int builtYear;
//Member functions
public:
//Default constructor
Ship();
//Parameterized constructor
Ship(string name,int year);
//Setters
void setName(string name);
void setYear(int year);
//Getters
string getName();
int getYear();
//print method
virtual void print();
};
Ship.cpp
//Ship implementation
#include "Ship.h"
//Default constructor
Ship::Ship() {
shipName = "";
builtYear = 1450;
}
//Parameterized constructor
Ship::Ship(string name, int year) {
shipName = name;
if (year >= 1450) {
builtYear = year;
}
else {
builtYear = 1450;
}
}
//Setters
void Ship::setName(string name) {
shipName = name;
}
void Ship::setYear(int year) {
if (year >= 1450) {
builtYear = year;
}
else {
builtYear = 1450;
}
}
//Getters
string Ship::getName() {
return shipName;
}
int Ship::getYear() {
return builtYear;
}
//print method
void Ship::print() {
cout << "Name: " << shipName <<
endl;
cout << "Year built: " << builtYear
<< endl;
}
cruiseShip.h
#include "Ship.h"
//Create a class CruiseShip
class CruiseShip : public Ship{
//Attributes
private:
int passengerCapacity;
//Member functions
public:
//Default constructor
CruiseShip();
//Parameterized constructor
CruiseShip(string name, int year,int capacity);
//Setters
void setCapacity(int capacity);
//Getters
int getCapacity();
//print method
virtual void print();
};
CruiseShip.cpp
#include "cruiseShip.h"
//Default constructor
CruiseShip::CruiseShip() {
passengerCapacity = 0;
}
//Parameterized constructor
CruiseShip::CruiseShip(string name, int year, int capacity)
:Ship(name, year) {
if (capacity < 0) {
passengerCapacity = 0;
}
else {
passengerCapacity = capacity;
}
}
//Setters
void CruiseShip::setCapacity(int capacity) {
if (capacity < 0) {
passengerCapacity = 0;
}
else {
passengerCapacity = capacity;
}
}
//Getters
int CruiseShip::getCapacity() {
return passengerCapacity;
}
//print method
void CruiseShip::print() {
cout << "Name: " << getName() <<
endl;
cout << "Maximum passengers: " <<
passengerCapacity << endl;
}
CargoShip.h
#pragma once
#include "Ship.h"
//Create a class CargoShip
class CargoShip : public Ship {
//Attributes
private:
int cargoCapacity;
//Member functions
public:
//Default constructor
CargoShip();
//Parameterized constructor
CargoShip(string name, int year, int capacity);
//Setters
void setCapacity(int capacity);
//Getters
int getCapacity();
//print method
virtual void print();
};
CargoShip.cpp
#include "CargoShip.h"
//Default constructor
CargoShip::CargoShip() {
cargoCapacity = 0;
}
//Parameterized constructor
CargoShip::CargoShip(string name, int year, int capacity)
:Ship(name, year) {
if (capacity < 0) {
cargoCapacity = 0;
}
else {
cargoCapacity = capacity;
}
}
//Setters
void CargoShip::setCapacity(int capacity) {
if (capacity < 0) {
cargoCapacity = 0;
}
else {
cargoCapacity = capacity;
}
}
//Getters
int CargoShip::getCapacity() {
return cargoCapacity;
}
//print method
void CargoShip::print() {
cout << "Name: " << getName() <<
endl;
cout << "Cargo capacity: " <<
cargoCapacity << " tons"<<endl;
}
Test.cpp
//Header files
#include "Ship.h"
#include "CargoShip.h"
#include "cruiseShip.h"
#include <iostream>
#include<string>
using namespace std;
//Test
int main()
{
//Create pointer array
Ship *arr[3] = { new Ship("Lollipop", 1960),new
CruiseShip("Disney magic", 1961, 2400),new CargoShip("Black pearl",
1962, 5000) };
//Display values
for (int i = 0; i < 3; i++) {
arr[i]->print();
cout <<
"---------------------" << endl;
}
}
Output
Name: Lollipop
Year built: 1960
---------------------
Name: Disney magic
Maximum passengers: 2400
---------------------
Name: Black pearl
Cargo capacity: 5000 tons
---------------------
Help needed on C++ program: Program Description: Complete the Ship, CruiseShip, and CargoShip program (#12 in...
QUESTION 5 (15 Marks) Inheritance and Polymorphism Design a Ship class that has the following members: • A private String data field named name for the name of the ship. • A private String data field named yearBuilt for the year that the ship was built. • A constructor that creates a ship with the specified name and the specified year that the ship was built. • Appropriate getter and setter methods. A toString method that overrides the toString method...
[JAVA] Program: Design a Ship class that the following members: A field for the name of the ship (a string) o A field for the year the the ship was built (a string) o A constructor and appropriate accessors and mutators A toString method that displays the ship's name and the year it was built Design a CruiseShip class that extends the Ship class. The CruiseShip class should have the following members: A field for the maximum number of passengers...
Python: Design a Ship class that has the following members: A member variable for the name of the ship. A member variable for the year that the ship was built. An init method and appropriate accessors and mutators. A 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....
Program Challenge 10 Design a Ship class that 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 displays the ship’s name and the year it was built. Design a CruiseShip class that extends the Ship class. The CruiseShip class should have the following members: · A field for the...
Ship, CruiseShip, and CargoShip Classes (in C++ language i use visual studios to code with) 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 contsructor and appropriate accessors and mutators - A virtual print function that displays the ship's name and the year it was built (nobody seems to get this part...
Design a Ship class that has the following class members: A field for the name of the ship A field for the year the ship was built A constructor Appropriate accessors and mutators toString method that displays the ships name and the year it was built Design a CruiseShip class the inherits from the Ship class. Include the following members: A field for the max number of passengers A constructor Appropriate accessors and mutators toString method that overrides the Ship toString method. The method...
using java write a code with notepad++ Create the following classes using inheritance and polymorphism Ship (all attributes private) String attribute for the name of ship float attribute for the maximum speed the of ship int attribute for the year the ship was built Add the following behaviors constructor(default and parameterized) , finalizer, and appropriate accessor and mutator methods Override the toString() method to output in the following format if the attributes were name="Sailboat Sally", speed=35.0f and year built of...
C++ program Correctly complete the following assignment. Follow all directions. The main purpose is to show super and sub class relationships with an array of super media pointers to sub class objects and dynamic binding. The < operator will be overloaded in the super class so all subclasses can use it. The selection sort method will be in the main .cpp file because it will sort the array created in main. The final .cpp file, the three .h header class...
HELP NEEDED in C++ (preferred) or any other language Write a simple program where you create an array of single byte characters. Make the array 100 bytes long. In C this would be an array of char. Use pointers and casting to put INTEGER (4 byte) and CHARACTER (1 byte) data into the array and pull it out. Make sure you can access both character and integer data at any location in the array. Read data from a text file....
C++ ONLY Please TRAINS DESCRIPTION You are completing a program that allows for several different types of passenger trains. One train can hold only cats. Another train can hold only wizards. You are going to create a Wizard class, a Cat class, and a Train class. You are provided the driver, lab2.cpp. The Train class should be a template class where the type of passenger is the template data type. Specifications cat class Attributes: name of cat breed of cat...