Dynamically Allocated an Array of Classes

I am trying to write a test file that dynamically allocates an array that i use to call the parent and two child classes. They will then call their own print functions.I d o not know if I am even doing it right but I keep getting the errors with the main every time. Even if i change something. They area all separate files in a project as well.

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
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138

class Ship
{
	protected:
		string shipName;
		string year;
	public:
		Ship();
		Ship(string, string);
		virtual void print();
};

Ship::Ship()
{
	shipName =  " ";
	year = " ";
}

Ship::Ship(string myName, string myYear) 	// Constructor
{
	//strcpy(shipName, myName);
	//strcpy(year, myYear);
	shipName = myName;
	year = myYear;
}

void Ship::print()
{
	cout << shipName << endl;
	cout << year << endl;
}

class CargoShip : public Ship
{
	protected:
		int load;		
	public:
		CargoShip();
		CargoShip(string, string, int);
		virtual void print();

};

CargoShip::CargoShip() : Ship()
{
	load = 0;
}

CargoShip::CargoShip(string myName, string myYear, int myLoad) :
	Ship(myName, myYear)
{
	load = myLoad;
}

void CargoShip::print()
{
	cout << shipName << endl;
	cout << load << endl;
}

class CruiseShip : public Ship
{
	protected:
		int passNum;
	
	public:
		CruiseShip();
		CruiseShip(string, string, int);
		virtual void print();

};

CruiseShip::CruiseShip() : Ship()
{
		passNum = 0;
}

CruiseShip::CruiseShip(string myName, string myYear, int myNum) :
	Ship(myName, myYear)
{
	passNum = myNum;
}

void CruiseShip::print()
{
	cout << shipName << endl;
	cout << passNum << endl;
}



#include "Ship.h"
#include "Cruise.h"
#include "Cargo.h"
#include <iostream>
#include <string>
using namespace std;

int main()
{

	shipArray[3]
	{
		shipArray[0] = new Ship("S.S. Shippy", "1997");
		shipArray[1] = new Cruise("S.S. La Cruise", "2017", 20);
		shipArray[2] = new Cargo("S.S El Cargo", "1845", 300);
	};
	
	shipArray[0].print();
	shipArray[1].print();
	shipArray[2].print();

	delete [] shipArray;
	
	/*CruiseShip cruise("S.S. La Cruise", "2017", 20);
	CargoShip cargo("S.S El Cargo", "1845", 300);
	
	ship.print();
	cruise.print();
	cargo.print();*/

/*	Ship ship("S.S. Shippy", "2017");
	ship.print();
		
	CruiseShip cruise("S.S. La Cruise", "2017", 20);
	cruise.print();
	
	CargoShip cargo("S.S El Cargo", "1845", 300);
	cargo.print();
*/	
	
	return 0;
}




Last edited on
Looks like you're trying to make an array of 3 Ship*. Three pointers.

The array you're creating here isn't dynamic. The objects being pointed to by each of the Ship* are.

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
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
#include <string>
#include <iostream>

using namespace std;

class Ship
{
	protected:
		string shipName;
		string year;
	public:
		Ship();
		Ship(string, string);
		virtual void print();
};

Ship::Ship()
{
	shipName =  " ";
	year = " ";
}

Ship::Ship(string myName, string myYear) 	// Constructor
{
	//strcpy(shipName, myName);
	//strcpy(year, myYear);
	shipName = myName;
	year = myYear;
}

void Ship::print()
{
	cout << shipName << endl;
	cout << year << endl;
}

class CargoShip : public Ship
{
	protected:
		int load;		
	public:
		CargoShip();
		CargoShip(string, string, int);
		virtual void print();

};

CargoShip::CargoShip() : Ship()
{
	load = 0;
}

CargoShip::CargoShip(string myName, string myYear, int myLoad) :
	Ship(myName, myYear)
{
	load = myLoad;
}

void CargoShip::print()
{
	cout << shipName << endl;
	cout << load << endl;
}

class CruiseShip : public Ship
{
	protected:
		int passNum;
	
	public:
		CruiseShip();
		CruiseShip(string, string, int);
		virtual void print();

};

CruiseShip::CruiseShip() : Ship()
{
		passNum = 0;
}

CruiseShip::CruiseShip(string myName, string myYear, int myNum) :
	Ship(myName, myYear)
{
	passNum = myNum;
}

void CruiseShip::print()
{
	cout << shipName << endl;
	cout << passNum << endl;
}

int main()
{

Ship* shipArray[3];

shipArray[0] = new Ship("S.S. Shippy", "1997");
shipArray[1] = new CruiseShip("S.S. La Cruise", "2017", 20);
shipArray[2] = new CargoShip("S.S El Cargo", "1845", 300);

shipArray[0]->print();
shipArray[1]->print();
shipArray[2]->print();

delete shipArray[0];
delete shipArray[1];
delete shipArray[2];

return 0;
}


https://ideone.com/0hwmB9
Last edited on
Thank you so much! I had something like that before but I couldn't get it to work the way i wanted it to. I got it to work with your solution. Thank you again!
Topic archived. No new replies allowed.