cannot instantiate abstract class?

I am working on a program that uses pure virtual methods, and I have gotten to a point where the same repeated errors appear, cannot instantiate abstract class errors.

1
2
3
4
5
6
7
8
c:\documents and settings\administrator\my documents\visual studio 2008\projects\week 6 i lab\week 6 i lab\vectorsexceptionhandling.cpp(426) : error C2259: 'ProfessorType' : cannot instantiate abstract class
due to following members:
'std::string PersonType::GetProgram(void)' : is abstract
c:\documents and settings\administrator\my documents\visual studio 2008\projects\week 6 i lab\week 6 i lab\vectorsexceptionhandling.cpp(51) : see declaration of 'PersonType::GetProgram'
'int EmployeeType::GetHoursWorked(void)' : is abstract
c:\documents and settings\administrator\my documents\visual studio 2008\projects\week 6 i lab\week 6 i lab\vectorsexceptionhandling.cpp(110) : see declaration of 'EmployeeType::GetHoursWorked'
'double EmployeeType::GetPayRate(void)' : is abstract
c:\documents and settings\administrator\my documents\visual studio 2008\projects\week 6 i lab\week 6 i lab\vectorsexceptionhandling.cpp(111) : see declaration of 'EmployeeType::GetPayRate'


Here is my program starting with the classes followed by the functions where the error occurs:
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
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
class PersonType
{
private:
string mFirstName;
string mLastName;
string mID;

public:

virtual char GetType(void) = 0;

PersonType::PersonType(string firstName, string lastName, string id)
{
mFirstName = firstName;
mLastName = lastName;
mID = id;
}

virtual void PersonType::Display()
{
cout << " Name: " << mLastName << ", " << mFirstName; 
cout << "\n ID: " << mID << endl;
}

string GetPersonID()
{
return mID;
}

string GetFirstName()
{
return mFirstName;
}

string GetLastName()
{
return mLastName;
}

virtual string GetProgram(void) = 0;
virtual string GetDept(void) = 0;
virtual string GetDateOfHire(void) = 0;
virtual int GetHoursWorked(void) = 0;
virtual double GetPayRate(void) = 0; 
virtual double GetSalary(void) = 0;
};

class StudentType : public PersonType
{
private:

string mProgram;

public:

char StudentType::GetType(void) {return 'S';}

StudentType::StudentType(string firstName, string lastName, string id, string program)
: PersonType(firstName, lastName, id)
{
mProgram = program; 
}
void StudentType::Display()
{
PersonType::Display();
cout << " Program: " << mProgram << endl << endl;
}

string StudentType::GetPersonID()
{
return PersonType::GetPersonID();
}

string StudentType::GetFirstName()
{
return PersonType::GetFirstName();
}

string StudentType::GetLastName()
{
return PersonType::GetLastName();
} 

string GetProgram()
{
return mProgram;
} 
};

class EmployeeType : public PersonType
{
private:
string mDept;
string mDateOfHire;

public:

virtual char EmployeeType::GetType() = 0;
virtual int EmployeeType::GetHoursWorked() = 0;
virtual double EmployeeType::GetPayRate() = 0; 
virtual double EmployeeType::GetSalary() = 0;

EmployeeType::EmployeeType(string firstName, string lastName, string id, 
string dept, string dateOfHire)
: PersonType(firstName, lastName, id)
{
mDept = dept;
mDateOfHire = dateOfHire;
}

void EmployeeType::Display()
{
PersonType::Display();
cout << " Dept: " << mDept << endl; 
cout << " Date Of Hire: " << mDateOfHire << endl;
}

string EmployeeType::GetPersonID()
{
return PersonType::GetPersonID();
}

string EmployeeType::GetFirstName()
{
return PersonType::GetFirstName();
}

string EmployeeType::GetLastName()
{
return PersonType::GetLastName();
} 

string EmployeeType::GetDept()
{
return mDept;
}

string EmployeeType::GetDateOfHire()
{
return mDateOfHire;
} 
};

class StaffType : public EmployeeType
{
private:
int mHoursWorked;
double mPayRate;

public:

char StaffType::GetType(void) {return 'F';}

StaffType::StaffType(string firstName, string lastName, string id, string dept, 
string dateOfHire, int hoursWorked, double payRate)
: EmployeeType(firstName, lastName, id, dept, dateOfHire)
{
mHoursWorked = hoursWorked;
mPayRate = payRate;
}

void StaffType::Display()
{
EmployeeType::Display();
cout << " Pay: $" << mPayRate * mHoursWorked << endl << endl; 
}

string GetPersonID()
{
return EmployeeType::GetPersonID();
}

string GetFirstName()
{
return EmployeeType::GetFirstName();
}

string GetLastName()
{
return EmployeeType::GetLastName();
} 

string GetDept()
{
return EmployeeType::GetDept();
}

string GetDateOfHire()
{
return EmployeeType::GetDateOfHire();
} 

int GetHoursWorked()
{
return mHoursWorked;
}

double GetPayRate()
{
return mPayRate;
} 
};

class ProfessorType : public EmployeeType
{
private:

double mSalary;

public:

char ProfessorType::GetType(void) {return 'P';}

ProfessorType::ProfessorType(string firstName, string lastName, string id, string dept, 
string dateOfHire, double salary)
: EmployeeType(firstName, lastName, id, dept, dateOfHire)
{
mSalary = salary;
}

void ProfessorType::Display()
{
EmployeeType::Display();
cout << " Salary: $" << mSalary << endl << endl; 
}

string GetPersonID()
{
return EmployeeType::GetPersonID();
}

string GetFirstName()
{
return EmployeeType::GetFirstName();
}

string GetLastName()
{
return EmployeeType::GetLastName();
}

string GetDept()
{
return EmployeeType::GetDept();
}

string GetDateOfHire()
{
return EmployeeType::GetDateOfHire();
} 

double GetSalary()
{
return mSalary;
} 
};
//...... more code
void AddNewProfessor(vector<PersonType*> &personVec)
{
string firstName = " ";
string lastName = " ";
string id = " ";
string dept = " ";
string dateOfHire = " ";
double salary = 0.00;

int idx = FindPerson(personVec, idx);
if(idx >= 0)
cout << "Enemy ID already used\n";
else
AddNewEmployee(firstName, lastName, id, dept, dateOfHire);

cout << "Enter Professor's Salary (a to abort): ";
cin >> salary;

PersonType *person = new ProfessorType(firstName, lastName, id, dept, dateOfHire, salary); // error occurs here 
for(idx = 0; idx < (int)personVec.size(); idx++)
{
if(id < personVec[idx]->GetPersonID())
{
personVec.insert(personVec.begin() + idx, person);
break;
}
}
if(idx == (int)personVec.size())
personVec.push_back(person);

}
Last edited on
char ProfessorType::GetType(void) {return 'P';}
This function is defined inline inside the class ProfessorType, so there is no need to prefix it with ProfessorType::

EDIT: Oh, I didn't see there was more than one pure virtual in the base class. Obviously, as CodeMonkey says below, make sure you've implemented all outstanding pure virtuals in any classes you want to instantiate.
Last edited on
closed account (1vRz3TCk)
It looks like you have not implemented all the virtual function in ProfessorType.
you seem to be missing GetProgram(),GetHoursWorked(), and GetPayRate()
ProfessorType is supposed to have the salary only, while the StaffType is supposed to have the HourWorked and the PayRate, and StudentType is supposed to have Program. It seems as if they should be similar to GetType() function. What am I missing?

Here are a few other errors I am getting along with the above one still.

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
c:\documents and settings\administrator\my documents\visual studio 2008\projects\week 6 i lab\week 6 i lab\vectorsexceptionhandling.cpp(462) : error C2259: 'StaffType' : cannot instantiate abstract class
        due to following members:
        'std::string PersonType::GetProgram(void)' : is abstract
        c:\documents and settings\administrator\my documents\visual studio 2008\projects\week 6 i lab\week 6 i lab\vectorsexceptionhandling.cpp(51) : see declaration of 'PersonType::GetProgram'
        'double EmployeeType::GetSalary(void)' : is abstract
        c:\documents and settings\administrator\my documents\visual studio 2008\projects\week 6 i lab\week 6 i lab\vectorsexceptionhandling.cpp(112) : see declaration of 'EmployeeType::GetSalary'
c:\documents and settings\administrator\my documents\visual studio 2008\projects\week 6 i lab\week 6 i lab\vectorsexceptionhandling.cpp(492) : error C2259: 'StudentType' : cannot instantiate abstract class
        due to following members:
        'std::string PersonType::GetDept(void)' : is abstract
        c:\documents and settings\administrator\my documents\visual studio 2008\projects\week 6 i lab\week 6 i lab\vectorsexceptionhandling.cpp(52) : see declaration of 'PersonType::GetDept'
        'std::string PersonType::GetDateOfHire(void)' : is abstract
        c:\documents and settings\administrator\my documents\visual studio 2008\projects\week 6 i lab\week 6 i lab\vectorsexceptionhandling.cpp(53) : see declaration of 'PersonType::GetDateOfHire'
        'int PersonType::GetHoursWorked(void)' : is abstract
        c:\documents and settings\administrator\my documents\visual studio 2008\projects\week 6 i lab\week 6 i lab\vectorsexceptionhandling.cpp(54) : see declaration of 'PersonType::GetHoursWorked'
        'double PersonType::GetPayRate(void)' : is abstract
        c:\documents and settings\administrator\my documents\visual studio 2008\projects\week 6 i lab\week 6 i lab\vectorsexceptionhandling.cpp(55) : see declaration of 'PersonType::GetPayRate'
        'double PersonType::GetSalary(void)' : is abstract
        c:\documents and settings\administrator\my documents\visual studio 2008\projects\week 6 i lab\week 6 i lab\vectorsexceptionhandling.cpp(56) : see declaration of 'PersonType::GetSalary'




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
class ProfessorType : public EmployeeType
{
private:

double mSalary;

public:

char ProfessorType::GetType(void) {return 'P';}

ProfessorType::ProfessorType(string firstName, string lastName, string id, string dept, 
string dateOfHire, double salary)
: EmployeeType(firstName, lastName, id, dept, dateOfHire)
{
mSalary = salary;
}

void ProfessorType::Display()
{
EmployeeType::Display();
cout << " Salary: $" << mSalary << endl << endl; 
}

string GetPersonID()
{
return EmployeeType::GetPersonID();
}

string GetFirstName()
{
return EmployeeType::GetFirstName();
}

string GetLastName()
{
return EmployeeType::GetLastName();
}

string GetDept()
{
return EmployeeType::GetDept();
}

string GetDateOfHire()
{
return EmployeeType::GetDateOfHire();
} 

double GetSalary()
{
return mSalary;
} 
};
//...... more code
void AddNewProfessor(vector<PersonType*> &personVec)
{
string firstName = " ";
string lastName = " ";
string id = " ";
string dept = " ";
string dateOfHire = " ";
double salary = 0.00;

int idx = FindPerson(personVec, idx);
if(idx >= 0)
cout << "Enemy ID already used\n";
else
AddNewEmployee(firstName, lastName, id, dept, dateOfHire);

cout << "Enter Professor's Salary (a to abort): ";
cin >> salary;

PersonType *person = new ProfessorType(firstName, lastName, id, dept, dateOfHire, salary); // error occurs here 
for(idx = 0; idx < (int)personVec.size(); idx++)
{
if(id < personVec[idx]->GetPersonID())
{
personVec.insert(personVec.begin() + idx, person);
break;
}
}
if(idx == (int)personVec.size())
personVec.push_back(person);

}
Last edited on
I understand from a design point of view that only a student has a program. However, you have a pure virtual function string PersonType::GetProgram(void) which is inherited by all other classes. The fact of the matter is that you have to implement all pure virtual functions for a class to be non abstract.

Think for a minute about your current code. If it compiled, you could do this:
1
2
PersonType* bob = new ProfessorType;
string prog = bob->GetProgram();

So what does the compiler do? bob is in fact of type ProfessrType, which does not implement PersonType::GetProgram(void).

The solution? Move string GetProgram(void) into class StudentType. This solves your problem. Moreover, it doesn't make sense to put the function in PersonType, since (as you pointed out), not all people have a program. So why should there be a means of finding out their program?

You may need to apply similar ideas to other members: don't give pure virtuals to bases whose (intended) non abstract children will not implement them.

PS: I suppose this is a matter of taste only, but do you really need to suffix every class name with 'Type'. A class is, by definition, a type ;)
Topic archived. No new replies allowed.