Alright I'm having problem with this program where I have three different classes but the functions don't seem to be working because of it being a pointer or I'm doing something wrong
#include <iostream>
#include <iomanip>
#include <string>
usingnamespace std;
//Global Variable
constint SIZE = 3;
class Ship
{
private:
string name;
string yearBuilt;
public:
Ship();
//Overloaded Constructor
Ship(string n, string y)
{
name = n;
yearBuilt = y;
}
//Accessors
string getName()
{return name;}
string getYearBuilt()
{return yearBuilt;}
//Mutator that gets info from user
virtualvoid setInfo()
{
string n;
string y;
cout <<"Please enter the ship name: ";
cin >>n;
cout <<"Please enter the year the ship was built: ";
cin >>y;
name = n;
yearBuilt = y;
}
//Print info for this function
virtualvoid print()
{
cout <<"Ship"<<endl;
cout <<"Name: "<<name<<endl
<<"Year Built: "<<yearBuilt<<endl;
}
};//end of ship
class CruiseShip : public Ship
{
private:
int passengers;
public:
CruiseShip();
//Overloaded constructor that has inherited variables
CruiseShip(string n, string y, int p): Ship(n,y)
{
passengers = p;
}
//Mutator that gets info from user
virtualvoid setInfo()
{
int p;
cout <<"Please enter the number of passengers: ";
cin >>p;
passengers = p;
}
//print function
virtualvoid print()
{
cout <<"Cruise Ship"<<endl;
cout <<"Name: "<<getName()<<endl
<<"Maximum Passanger: "<<passengers<<endl;
}
};// end of CruiseShip
class CargoShip : public Ship
{
int tonnage;
public:
CargoShip();
//Overloaded constructor that has inherited variables
CargoShip(string n, string y, int t):Ship(n,y)
{
tonnage = t;
}
//Mutator that gets info from user
virtualvoid setInfo()
{
int t;
cout <<"Please enter the cargo capacity: ";
cin >>t;
tonnage = t;
}
//Print info for this class
virtualvoid print()
{
cout <<"Cargo Ship"<<endl;
cout <<"Name: "<<getName()<<endl
<<"Cargo Capacity: "<<tonnage<<endl;
}
}; //end of CargoShip
int main()
{
Ship *ships[SIZE] = {new Ship(), new CruiseShip(), new CargoShip()};
int index; //Used with For loops
for(index = 0; index < SIZE; index++)
{
*ships[index]->setInfo();
cout <<endl;
}
for(index = 0; index < SIZE; index++)
{
*ships[index]->print();
cout <<endl;
}
return 0;
}
Well it needs to be a array of Ship pointers where the array elements should be initialized with the address of dynamically allocated objects of the three classes.
After playing around with it for a while I got it to work, but it's not quite as versatile as I originally wanted, but it works for its purpose. Thanks for the help.