Ship Class Misunderstanding Problem

Ok so I'm stuck with a few errors from VB and not sure how to sort them out. Here is my code...

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
#ifndef SHIPH
#include "Ship.cpp"
#include <iostream>
#include <string>



using namespace std;



class Ship
{
private:
	string year;
	string name;

public:
	int SIZE;
	Ship() {};
	

	Ship(string n, string y)
	{
		name = n;
		year = y;
	}
	
	
	string getName()
	{
		return name;
	}
	
	string getYear()
	{
		return year;
	}
	
		
	virtual void setInfo()
	{
		string n;
		string y;
		cout <<"Please enter the name of the ship: ";
		cin >>n;
		cout <<"Please enter the year of the ship: ";
		cin >>y;
		name = n;
		year = y;
	}

	virtual void print()
	{
		cout << "Ship Name: " << name <<endl;
		
		cout << "Ship Year: " << year <<endl;
	}
};
	
#endif 


1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
#include <iostream>
#include <string>
using namespace std;

class Ship
{
    
    string name;
    int year;
    
    
    
public:
    
    Ship(string shipName,int shipYear)
    {
        
        name = shipName;
        year = shipYear;
        
    }
    
    
    string getShipName()
    {
        
        return name;
    }
    
    
    void setShipName(string setName)
    {
        
        name = setName;
        
    }
    
    
    int getYearBuilt()
    {
        
        return year;
        
    }
    
    
    
    void setYear(int setYear)
    {
        
        year = setYear;
        
    }
    
    
    virtual void print()
    {
        
cout << "Ship name: " << name << "\nYear built: " << year << endl;
        
    }
    
};

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
#ifndef CRUISESHIPH
#include "CruiseShip.cpp"
#include "Ship.cpp"
#include "Ship.h"
#include <iostream>
#include <string>



using namespace std;



class CruiseShip : public Ship
{
private:
	int passengers;
public:
	int SIZE;
	CruiseShip();
	
	CruiseShip(string n, string y, int p): Ship(n,y)
	{
		passengers = p;
	}
	
	virtual void setInfo()
	{
		int p;
		cout << "Please enter the number of passengers on the Cruise Ship: ";
		cin >>p;
		passengers = p;
	}
	
	virtual void print()
	{
		cout << "Cruise Ship: "<< getName() << endl;
		cout << "Maximum passangers for Cruise Ship: " << getName() << " is" << passengers << " passengers." << endl;
	}
};
#endif

CruiseShip::CruiseShip()
{
}

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
#include <iostream>
#include <string>
#include "Ship.cpp"
using namespace std;




class CruiseShip : public Ship
{
    
    int passengers;
    
    
    
public:
    
    CruiseShip(string name, int year, int pass) : Ship(name,year)
    {
        passengers = pass;
        
    }
    
    
    void setPassengerCapacity(int cap)
    {
        
        passengers = cap;
        
    }
    
    
    int getPassengerCapacity()
    {
        
        return passengers;
        
    }
    
    
    
    void print()
    {
        
cout << "Cruise ship name: " << getShipName() << "\nMaximum passengers: " << passengers << endl;
        
    }
    
};

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
#ifndef CARGOSHIPH
#include "CargoShip.cpp"
#include "Ship.cpp"
#include "Ship.h"
#include <iostream>
#include <string>



using namespace std;



class CargoShip : public Ship
{
private:
	int capacity;
public:
	int SIZE;
	CargoShip();
	
	CargoShip(string n, string y, int c): Ship(n,y)
	{
		capacity = c;
	}
	
	virtual void setInfo()
	{
		int c;
		cout << "In tonnage, enter amount of cargo is on the Cargo Ship: ";
		cin >>c;
		capacity = c;
	}
	//print function
	virtual void print()
	{
		cout << "Cargo Ship: "<< getName() << endl;
		cout << "Maximum tonnage for Cargo Ship: " << getName() << " " << capacity << " tons." << endl;
	}
};
#endif 

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
#include <iostream>
#include <string>
#include "Ship.cpp"
using namespace std;


class CargoShip : public Ship
{
    
    int capacity;
	const int SIZE = 3;
    
    
public:
    
    CargoShip(string cargoName, int cargoYear, int cargoP) : Ship(cargoName,cargoYear)
    {
        
        capacity = cargoP;
        
    }
    
    
    void setCapacity(int cap)
    {
        
        capacity = cap;
        
    }
    
    
    int getCapacity()
    {
        
        return capacity;
        
    }
    
    
    void print()
    {
        
cout << "Cargo ship name: " << getShipName() << "\nCargo: " << capacity << endl;
        
    }
    
};

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
#include "Ship.cpp"
#include "CruiseShip.cpp"
#include "CargoShip.cpp"
#include <iostream>
#include <string>
#include <cstdlib>
#include <iomanip>
#include <fstream>


using namespace std;

int main{

	// Create an array of Ship pointers, initialized with
	 // the addresses of some dynamically allocated objects.
	 const int SIZE = 3;
	 Ship *ships[SIZE] = { new Ship ("Lolipop", 1960),
	 new CruiseShip("Disney Magic", 1998, 2400),
	 new CargoShip("Black Pearl", 1800, 50000) };
	 int firstShip = 0;
	 int lastShip = 0;
	 // Call each object's print function and check for newest and oldest.
	 for (int index = 0; index < SIZE; index++)
	 {
	 ships[index]->print();
	 cout << "----------------------------\n";
	 //Use overloaded relational operators
		if (*ships[index] < *ships[firstShip])
		firstShip = index;
		if (*ships[index] > *ships[lastShip])
		lastShip = index;
	 }
	 cout << endl;
	 cout << "The oldest ship built was in " << ships[firstShip]->getYearBuilt() << endl;
	 ships[firstShip]->print();
	 cout << "The newest ship built was in " << ships[lastShip]->getYearBuilt() << endl;
	 ships[lastShip]->print();
	 //free up the memory
	 for (int index = 0; index < SIZE; index++)
	 {
		delete ships[index];
	 }
	 system("pause");
	 
}


Any help would be appreciated.
Last edited on
For starters, you should not #include .cpp files, you #include .h files.

you also seem to be doing this:
class CargoShip : public Ship
in both your .cpp AND your .h, which is very wrong.

I suggest reading the tutorials on classes on this website.
In addition, if you're using macro inclusion guards, you need to #define the macros as well, not just check if they haven't been defined yet.

1
2
#ifndef SHIPH
/**/


would become:

1
2
3
#ifndef SHIPH
#define SHIPH
/**/


And so on.
Last edited on
> with a few errors
be especific.


> Here is my code...
I'm not going to bother to reproduce your file structure. Upload to github or provide a zip.
Also, you have defined two different Ship classes, in one of which year is a string and in the other year is an int.
Topic archived. No new replies allowed.