Deletion of a pointer array of parent class (Inheritance)

Hello, following is the test program I created.
I have tried to dynamically create objects of derived classes of the base class "person" by using a pointer array of 100 size..
But, the program is giving RunTime error whenever it comes to deletion of the pointer array. Now, I don't understand why is this happening.. Can anyone explain, please? Thank you!

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
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
#include<iostream>
#include<string>
using namespace std;
class person
{
protected:
	string name;
	int id;
public:
	virtual void input()
	{
		cin >> name >> id;
	}
	virtual void print()
	{
		cout << name << id;
	}
	~person()
	{}
};
class student : public person
{
protected:
	float cgpa;
public:
	student()
	{
		cgpa = 0.0;
	}
	void input()
	{
		person::input();
		cin >> cgpa;
	}
	void print()
	{
		person::print();
		cout << cgpa;
	}
	~student(){}
};
class undergraduate : public student
{
protected:
	float sgpa;
public:
	void input()
	{
		person::input();
		cin >> cgpa;
		cin >> sgpa;
	}
	void print()
	{
		person::print();
		cout << cgpa;
		cout << sgpa;
	}
	~undergraduate()
	{}
};

class graduate : public student
{
	float credits;
public:
	void input()
	{
		person::input();
		cin >> cgpa;
		cin >> credits;
	}
	void print()
	{
		person::print();
		cout << cgpa;
		cout << credits;
	}
	~graduate()
	{

	}
};
class faculty : public person
{
protected:
	int salary;
public:
	faculty()
	{
		salary = 0;
	}
	void input()
	{
		person::input();
		cin >> salary;
	}
	void print()
	{
		person::print();
		cout << salary;
	}
	~faculty()
	{}
};
class staff : public person
{
protected:
	int salary;
public:
	staff()
	{
		salary = 0;
	}
	void input()
	{
		person::input();
		cin >> salary;
	}
	void print()
	{
		person::print();
		cout << salary;
	}
	~staff()
	{}
};
int main()
{
	person *p[100];
	int n, n1;
	/*for (int i = 0; i < 3; i++)
	{
		cin >> n; 
		switch (n)
		{
		case 1:
			p[i] = new student;
			cin >> n1;
			switch (n1)
			{
			case 1:
				p[i] = new undergraduate;
				p[i]->input();
				p[i]->print();
			case 2:
				p[i] = new graduate;
				p[i]->input();
				p[i]->print();
				break;
			}
		case 2:
			p[i] = new faculty;
			p[i]->input();
			p[i]->print();
			break;
		case 3:
			p[i] = new staff;
			p[i]->input();
			p[i]->print();
			break;
		default:
			p[i] = NULL;
			break;
		}

	}*/
	delete[]p;


	system("pause");
	return 0;
}

You can only delete something that is created using new.

Let's see how p is created:
person *p[100];

Is p created using new? No.
So can we delete it? No.
and maybe more fundamentally:
p is an array of pointers.
p is not, itself, a pointer at all!

conceptually, you have this:
unsigned long long p[100];
where p[x] is a number that just happens to be the address in ram of some other variable.

you wouldn't think to use delete[] on p if it were just an array of long long, right? But that is exactly what it really IS. The syntax is tripping you up because it has an * in it maybe. It takes some getting used to.
one last time, just to be extra clear:
(person *) p[100]; that is the TYPE of the array, its an array of pointers to person.
so the array contains pointers, but isnt, itself, a pointer.

person* p; //this one IS a pointer itself. its the array-ness of the above that changes how to read it, a bit. you would still need a new on this if you wanted to delete it.

back to the original,
p[x] IS a pointer.
if you had happened to code
p[x] = new thingy[somesize];
then
delete[] p[x]; would be a valid thing to do.

if you had declared p as thingy **p;
p = new thingy*[100];
now you can delete p, because p is now actually a pointer that is doing the same thing as your array (there is no reason to do this if 100 is a fixed size you want to stick to).

I suggest you put this program down and play with pointers in a 10 line main program for a few min to get a better handle on the syntax and truth behind what you are trying to do. Trying to do something bigger while using pointers while not understanding them fully is a recipe for disaster. And doubly so when trying to do this in classes with inheritance and other complexities.


Last edited on
Last edited on
Topic archived. No new replies allowed.