I am trying to get my code to display the Ship class data, then use pointers to display the updated data after enter in console and set to the relevant variables in the CruiseShip and CragoShip classes. However, the program will ignore any new input and simply display the base class data again regardless of new data.
What is missing here?
using namespace std;
// -- Initialized the class 'Ship' -- //
class Ship {
public:
int yearBuilt;
string nameOfShip;
// Constructs the ship class and defines the values of the variables
Ship() {
yearBuilt = 0;
nameOfShip = "";
}
// Overloaded Constructor
Ship(int year, string name) {
Ship::yearBuilt = year;
Ship::nameOfShip = name;
year = 0;
name = "";
}
// Accessor Function for the year the ship was built
int getYearBuilt() const {
return yearBuilt;
}
// Accessor Function for the name of the ship
string getShipName() const {
return nameOfShip;
}
// Mutator Function to read input and set it to the value of yearBuilt
void setShipYear(int year) {
cout << " Enter Year: " << endl;
cin >> year;
year = yearBuilt;
cout << endl; // Spacing between printed data
}
// Mutator Function to read input and set it to the value of nameOfShip
void setShipName(string name) {
cout << " Enter Name: " << endl;
cin >> name;
name = nameOfShip;
cout << endl; // Spacing between printed data
}
// Virtual Print function to display the name of the ship and the year it was built
virtual void print() const {
cout << " Ship Name: " << nameOfShip << endl;
cout << " Year Built: " << yearBuilt << endl;
}
};
// -- Initializes the class 'CruiseShip' derived from Ship class -- //
class CruiseShip : public Ship
{
// Set the member variable for max number of passengers
int passengersMAX;
public:
//Constructor for CruiseShip, calls parent class
CruiseShip() : Ship() {
passengersMAX = 0;
}
//Overloaded Constructor
CruiseShip(int maximum, int year, string name) : Ship() {
CruiseShip::passengersMAX = maximum;
}
//Accessor
int getMaxPass() const {
return passengersMAX;
}
//Mutator
void setMaxPass(int maximum) {
cout << "Enter Passenger Max: " << endl;
cin >> maximum;
maximum = passengersMAX;
}
//Overriding Print Function
virtual void print() const override{
cout << " Ship Name: " << nameOfShip << endl;
cout << " Max number Of Passengers: " << passengersMAX << endl;
}
};
class CargoShip : public Ship
{
// Set the member variable for tonnage / capacity
int capacity;
public:
// Default Constructor
CargoShip() : Ship() {
capacity = 0;
}
//Overloaded constructor for CargoShip, calls parent class
CargoShip(int tonnage, string name) : Ship() {
CargoShip::capacity = tonnage;
}
// Accessor Function
int getCapacity() const {
return capacity;
}
//Mutator Function
void setCapacity(int tonnage) {
cout << " Enter max capacity: " << endl;
cin >> tonnage;
tonnage = capacity;
}
//Overriding Print Function
virtual void print() const override{
cout << " Name: " << nameOfShip << endl;
cout << " Capacity: " << capacity << endl;
}
};
int main()
{
// Pointer Array for Ships, listing the 3 ship classes
Ship *shipArray[3] = { new Ship(), new CruiseShip(), new CargoShip() };
// For loop to print the data for each of the 3 ships
for (int i = 0; i < 3; i++)
{
shipArray[i]->print();
cout << endl;
}
// Stores new data using the mutator functions within the class
shipArray[0]->setShipName("RMS Titanic");
shipArray[0]->setShipYear(1909);
// Pointers to the derived class, stores new data for functions in CruiseShip class
CruiseShip *csPoint = static_cast<CruiseShip*>(shipArray[1]);
csPoint->setShipName("HMS Victory");
csPoint->setMaxPass(850);
// Pointer to the derived class, stores new data for functions in CargoShip class
CargoShip *cgPoint = static_cast<CargoShip*>(shipArray[2]);
cgPoint->setShipName("HMHS Britannic");
cgPoint->setCapacity(48158);
//For loop to re-display updated data using base class pointers
for (int i = 0; i < 3; i++) {
shipArray[i]->print();
cout << endl;
}
return 0;
}