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.

ships is declared as an array of pointers but on both lines 17 and 23 having selected the array index you dereference the array once with * and once with -> or twice in total.

You can dereference a single pointer twice, loose the * on both lines.


BTW I suspect you want the derived class mutators to call the base class mutator as part of their operation.

ships is declared as an array of pointers but on both lines 17 and 23 having selected the array index you dereference the array once with * and once with -> or twice in total.

You can dereference a single pointer twice, loose the * on both lines.


BTW I suspect you want the derived class mutators to call the base class mutator as part of their operation.

I tried the following you mention

code:

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;
}

Now i get even more errors I am unsure about.

Error 1 error LNK2028: unresolved token (0A000295) "public: __thiscall Ship::Ship(void)" (??0Ship@@$$FQAE@XZ) referenced in function "int __cdecl main(void)" (?main@@$$HYAHXZ) problem 12.obj chapter 15

Error 2 error LNK2028: unresolved token (0A0002A2) "public: __thiscall CruiseShip::CruiseShip(void)" (??0CruiseShip@@$$FQAE@XZ) referenced in function "int __cdecl main(void)" (?main@@$$HYAHXZ) problem 12.obj chapter 15

Error 3 error LNK2028: unresolved token (0A0002B0) "public: __thiscall CargoShip::CargoShip(void)" (??0CargoShip@@$$FQAE@XZ) referenced in function "int __cdecl main(void)" (?main@@$$HYAHXZ) problem 12.obj chapter 15

Error 4 error LNK2019: unresolved external symbol "public: __thiscall CargoShip::CargoShip(void)" (??0CargoShip@@$$FQAE@XZ) referenced in function "int __cdecl main(void)" (?main@@$$HYAHXZ) problem 12.obj chapter 15

Error 5 error LNK2019: unresolved external symbol "public: __thiscall CruiseShip::CruiseShip(void)" (??0CruiseShip@@$$FQAE@XZ) referenced in function "int __cdecl main(void)" (?main@@$$HYAHXZ) problem 12.obj chapter 15

Error 6 error LNK2019: unresolved external symbol "public: __thiscall Ship::Ship(void)" (??0Ship@@$$FQAE@XZ) referenced in function "int __cdecl main(void)" (?main@@$$HYAHXZ) problem 12.obj chapter 15

Error 7 fatal error LNK1120: 6 unresolved externals C:\Documents and Settings\c1170823\Desktop\Winter Quarter\CIS234\week9\chapter 15\Debug\chapter 15.exe chapter 15


Suggestions?

You need to #include ship.h in cruiseship.h and cargoship.h. Without that inclusion, the Ship base class is completely unknown to them.

Also, try changing name and year in your ship class to protected instead of private. With them being private, nothing but methods internal to ship can access them. Protected still hides them from outside influence, but relaxes restrictions for derived classes.

Notes on Inheritance.
Notes on Polymorphism.

Edited 6 Years Ago by Fbody: n/a

You need to #include ship.h in cruiseship.h and cargoship.h. Without that inclusion, the Ship base class is completely unknown to them.

Also, try changing name and year in your ship class to protected instead of private. With them being private, nothing but methods internal to ship can access them. Protected still hides them from outside influence, but relaxes restrictions for derived classes.

Notes on Inheritance.
Notes on Polymorphism.

I did include #include "ship.h", etc. I included it on the main cpp.:

#include <iostream>
#include <iomanip>
#include <string>
#include "ship.h"
#include "cruiseShip.h"
#include "cargoShip.h"

using namespace std;

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;
}

If i include them on the header of cruise and cargo, i get errors since I already included ship.h into the other headers with line 4:

class CargoShip : public Ship

so where do i include the other #include "_____"s?

You're correct, that should be okay. The other errors were the result of not having proper header guards in place.

Error 1 error LNK2028: unresolved token (0A000295) "public: __thiscall Ship:hip(void)" (??0Ship@@$$FQAE@XZ) referenced in function "int __cdecl main(void)" (?main@@$$HYAHXZ) problem 12.obj chapter 15

These types of errors are the result of the linker not being able to locate the necessary function implementations. They are not compilation errors.

You have declared and defined your functions in just the header files, and not in separate header and source files, but you didn't implement your default constructors correctly. As a result, the linker can't find the implementations for them. You have created prototypes, but you haven't implemented them.

//this is a declaration/prototype:
Ship();

//this is a proper implementation/definition:
Ship(){}

Per my tests, you'll have to change all 3 of your default constructors to the proper format, then it will work.

Edited 6 Years Ago by Fbody: n/a

This article has been dead for over six months. Start a new discussion instead.