Back again, with another homework problem that is stumping me. Ok so assignment is to design 3 classes: Ship, Cruise Ship, and Cargo Ship.
Ship should:
member variable for name of ship (string)
member variable for year ship was built (string)
constructor and accessors and mutators
virtual print that displays ship name and year it was build
Cruise ship should:
derived from Ship Class.
member variable for max. # of passengers (int)
constructor accessors and mutators.
A print function that overrides the print function in the base class. cruise shop print function should display only the sip name and max # of passengers.
Cargo Ship should:
derived from ship class.
member varialbe for cargo capcity in tonnage (int)
constructon, accessors, and mutators.
print function that overrides the print function in base class. should only print ship's name and capacity.
demonstrate the classes in a program that has an array of ship points. array of elements should initialize with the addresses of dynamically allocated Ship, CruiseShip, and CargoShip objects. program should then step through the array, calling each object's print function.
Heres my code so far:
Ship Class (ship.h):
#include <string>
class Ship
{
private:
string name;
string year;
public:
Ship();
//Overloaded Constructor
Ship(string n, string y)
{
name = n;
year = y;
}
//Accessors
string getName()
{return name;}
string getyear()
{return year;}
//Mutator that gets info from user
virtual void 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;
year = y;
}
//Print info for this function
virtual void print()
{
cout <<"Ship"<<endl;
cout <<"Name: "<<name<<endl
<<"Year Built: "<<year<<endl;
}
};
CruiseShip.h code:
#include<string>
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
virtual void setInfo()
{
int p;
cout <<"Please enter the number of passengers: ";
cin >>p;
passengers = p;
}
//print function
virtual void print()
{
cout <<"Cruise Ship"<<endl;
cout <<"Name: "<<getName()<<endl
<<"Maximum Passanger: "<<passengers<<endl;
}
};// end of CruiseShip
CargoShip.h code:
#include<string>
class CargoShip : public Ship
{
int capacity;
public:
CargoShip();
//Overloaded constructor that has inherited variables
CargoShip(string n, string y, int t):Ship(n,y)
{
capacity = t;
}
//Mutator that gets info from user
virtual void setInfo()
{
int t;
cout <<"Please enter the cargo capacity: ";
cin >>t;
capacity = t;
}
//Print info for this class
virtual void print()
{
cout <<"Cargo Ship"<<endl;
cout <<"Name: "<<getName()<<endl
<<"Cargo Capacity: "<<capacity<<endl;
}
}; //end of CargoShip
Main CPP;
#include <iostream>
#include <iomanip>
#include <string>
using namespace std;
#include "ship.h"
#include "cruiseShip.h"
#include "cargoShip.h"
const int SIZE = 3;
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;
}
The program will not compile. Error:
Error 1 error C2100: illegal indirection Line: 17
Error 2 error C2100: illegal indirection Line 23
Any help would be great! Have been starring at this code for a few days now. Granted, much of the code is from the book examples and me trying to use it for the assignment way (as most students do). So please, if my intial code needs re-worked just say so. If you can help me just element the error and get the code to compile (corss fingers it works) THAT'D BE GREAT.
Thanks in advance to anyone who replies with any help whatsoever. I appreicate your time and effort as this place seems to always have someone that can help.