Student GPA Inheritance Program Problem.

I am new to C++ programming and I have working with program for days now. This program using inheritance techniques. The asks the user to "Enter 1 for undergrad or 2 for gard student and than displays input information according. In advance, thank you for any help that someone can provide.


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
  #include <iostream>
#include <iomanip>
#include <string>
using namespace std;

class Student
{
protected:
	
	int social;
	char name;
	char *NamePtr;
public:
	Student (); // Default constructor
	Student(int ssn, char  StudentName);
	void setSSN(int);
	int getSSN();
	void setName(string);
	char* getName();
	virtual double calcGPA();
};

Student ::Student()
{
}
Student::Student(int ssn, char  StudentName)
{
	
	name = StudentName;
	social = ssn;
	
	return;
}

void Student::setSSN(int SSN)
{
		social = SSN;
		return;
}

int Student::getSSN()
{
	return (social);
}

void Student::setName(char name2)
{
		name = name2;
		return;
}

char* Student::getName()
{	
	NamePtr = name;
	return (NamePtr);
}

double Student::calcGPA()
{
	cout << "Student calcGPA" << endl;
	return (0);
}

class GradStudent : public Student 
{
private:
	char status;
public:
	GradStudent (char = 'A');
	virtual double calcGPA();
	void SetStatus(char);
};

GradStudent::GradStudent(char grade)
{
	status = grade;
}

double GradStudent::calcGPA()
{
	double GPA = 0.0;
	if (status == 'A' || status == 'a')
		GPA = 4.0;
	cout << "GradStudent calcGPA";
	return (GPA);
}

void GradStudent::SetStatus (char stat)
{
	status = stat;
	return;
}

class UnderGradStudent : public Student
{
private:
	double CreditHrs;
	double QualityPts;
public:
	UnderGradStudent ( double = 0, double = 0);
	virtual double calcGPA ();
	void setCredits(int);
	void setQuality(int);
};

UnderGradStudent::UnderGradStudent(double Hours, double Points)
{
	CreditHrs = Hours;
	QualityPts = Points;
	if (Hours < 0)
		CreditHrs = 0;
	if (Points < 0)
		QualityPts = 0;
}

double UnderGradStudent::calcGPA ()
{
	double GPA = 0.0;
	if (CreditHrs > 0)
		GPA = QualityPts / CreditHrs;
	cout << "UnderGraduateStudent calcGPA";
	return (GPA);
}

void UnderGradStudent::setCredits (int C)
{
	CreditHrs = C;
	if (C < 0)
		CreditHrs = 0;
	return;
}

void UnderGradStudent::setQuality (int Q)
{
	QualityPts = Q;
	if (Q < 0)
		QualityPts = 0;
	return;
}


int main()
{
	char ans = 'n', grade;
	string StudentName;
	int StudentSSN;
	int code, credits, points;

	
	Student* a = NULL;

	do
	{
		cout << "Enter 1 for undergrad.\nEnter 2 for grad." << endl << "-->";
		cin >> code;
	
		if (code == 1)
		{
			a = new UnderGradStudent;

			cin.ignore();
			cout << "Enter Student name: ";
			cin.getline(StudentName);
			a->setName (StudentName);
			cout << "Enter Student SSN: ";
			cin >> StudentSSN;
			a->getSSN();

			cout << "Enter credit hours: ";
			cin >> credits;
			a->setCredits(credits);

			cout << "Enter quality points: ";
			cin >> points;
			a->setQuality(points);
			cout << endl;
			
			
		}

		else if (code == 2)
		{
			a = new GradStudent;
			
			cin.ignore();
			cout << "Enter Student name: ";
			a->getline(StudentName);
			
			
			cout << "Enter Student SSN: ";
			cin >> StudentSSN;
			a->setSSN(StudentSSN);

			cout << "Enter Status: ";
			cin >> grade;
			a->SetStatus(grade);
			cout << endl;
		}

		
		

		
		cout <<  "\nName: " << a->getName() << " SSN: " << a->getSSN() << " GPA: "
			<< setiosflags(ios::fixed | ios::showpoint) << setprecision(2) << a->calcGPA() << endl;

		
		delete a;
		a = NULL;

		cout << "Continue?: ";
		cin >> ans;
		cin.ignore();
		cout << endl;
	}
	while (toupper(ans) == 'Y');

	

	return 0;
}
You have a quite a few problems here. For example:

Compare line 18 (member function declaration)
void setName(string);
with line 46 (member function definition)
void Student::setName(char name2)

The arguments to the function need to be the same - this is an easy mistake to make, but it's also easy to diagnose once you get used to reading the compiler error messages & looking at the code the message is complaining about.

The majority of your problems boil down to declaring things as char when you seem to actually want to be using a string . Remember that a char is a single character, whereas a string is a container for many characters.

My advice would be to look at every instance of char in you code and ask yourself, Do I mean this to be a single character, or do I want to store many characters?
Last edited on
Being new to C++, some of the error messages are kind of confusing to me.

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
#include <iostream>
#include <iomanip>
#include <string>
using namespace std;

class Student
{
protected:
	
	int social;
	string name;
	string *NamePtr;
public:
	Student (); // Default constructor
	Student(int ssn, string StudentName);
	void setSSN(int);
	int getSSN();
	void setName(string);
    string* getName();
	virtual double calcGPA();
};

Student ::Student()
{
}
Student::Student(int ssn, string  StudentName)
{
	
	social = ssn;
	strcpy (name, Studentname);
	
	return;
}

void Student::setSSN(int SSN)
{
		social = SSN;
		return;
}

int Student::getSSN()
{
	return (social);
}

void Student::setName(string name2)
{
		strcpy (name, name2);
		return;
}

string* Student::getName()
{	
	NamePtr = name;
	return (NamePtr);
}

double Student::calcGPA()
{
	cout << "Student calcGPA" << endl;
	return (0);
}

class GradStudent : public Student 
{
private:
	char Status;
public:
	GradStudent (char = 'A');
	virtual double calcGPA();
	void SetStatus(char);
};

GradStudent::GradStudent(char grade)
{
	Status = grade;
}

double GradStudent::calcGPA()
{
	double GPA = 0.0;
	if (Status == 'A' || Status == 'a')
		GPA = 4.0;
	cout << "GradStudent calcGPA";
	return (GPA);
}

void GradStudent::SetStatus (char stat)
{
	Status = stat;
	return;
}

class UnderGradStudent : public Student
{
private:
	double CreditHrs;
	double QualityPts;
public:
	UnderGradStudent ( double = 0, double = 0);
	virtual double calcGPA ();
	void setCreditHrs(int);
	void setQuality(int);
};

UnderGradStudent::UnderGradStudent(double Hours, double Points)
{
	CreditHrs = Hours;
	QualityPts = Points;
	if (Hours < 0)
		CreditHrs = 0;
	if (Points < 0)
		QualityPts = 0;
}

double UnderGradStudent::calcGPA ()
{
	double GPA = 0.0;
	if (CreditHrs > 0)
		GPA = QualityPts / CreditHrs;
	cout << "UnderGraduateStudent calcGPA";
	return (GPA);
}

void UnderGradStudent::setCreditHrs (int C)
{
	CreditHrs = C;
	if (C < 0)
		CreditHrs = 0;
	return;
}

void UnderGradStudent::setQuality (int Q)
{
	QualityPts = Q;
	if (Q < 0)
		QualityPts = 0;
	return;
}


int main()
{
	char ans = 'n', grade, Status;
	string StudentName;
	int StudentSSN;
	int code, CreditHrs, points;

	
	Student* a = NULL;

	do
	{
		cout << "Enter 1 for undergrad.\nEnter 2 for grad." << endl << "-->";
		cin >> code;
	
		if (code == 1)
		{
			a = new UnderGradStudent;

			cin.ignore();
			cout << "Enter Student name: ";
			cin.getline(StudentName);
			a->setName (StudentName);
			cout << "Enter Student SSN: ";
			cin >> StudentSSN;
			a->getSSN();

			cout << "Enter credit hours: ";
			cin >> CreditHrs;
			a->setCreditHrs(CreditHrs);

			cout << "Enter quality points: ";
			cin >> points;
			a->setQuality(points);
			cout << endl;
			
			
		}

		else if (code == 2)
		{
			a = new GradStudent;
			
			cin.ignore();
			cout << "Enter Student name: ";
			a->getName(StudentName);
			
			
			cout << "Enter Student SSN: ";
			cin >> StudentSSN;
			a->setSSN(StudentSSN);

			cout << "Enter Status: ";
			cin >> Status;
			a->SetStatus(Status);
			cout << endl;
		}

		
		

		
		cout <<  "\nName: " << a->getName() << " SSN: " << a->getSSN() << " GPA: "
			<< setiosflags(ios::fixed | ios::showpoint) << setprecision(2) << a->calcGPA() << endl;

		
		delete a;
		a = NULL;

		cout << "Continue?: ";
		cin >> ans;
		cin.ignore();
		cout << endl;
	}
	while (toupper(ans) == 'Y');

	

	return 0;
}
Line 30: Studentname capitalization is incorrect.

Line 30,47: You can't use the C strcpy function to copy C++ std::string objects. Simply use the assignment operator.
 
  name = StudentName;


Line 52: Really not recommended to return a pointer to a string. Either return a copy of the string, or a reference to it. getName() should be const. There is no reason for a getter to modify the object. That also means get rid of namePtr.
1
2
3
string Student::getName() const
{  return name; 
}


line 163: The correct form of getline is:
 
    getline(cin, StudentName);


Line 167: Why are you calling a getter here. Didn't you mean to call a setter?

Line 171: setCreditHrs is not a member of Student. a is type Student, even though you did a new UnderGradStudent.
You can do this instead:
1
2
3
4
5
    UnderGradStudent *  ug;
    ug = new UnderGradStudent;
    a = ug;
...
ug->setCreditHrs(CreditHrs);


Line 187: You want the setter here, not the getter.



I have been at this program for four hours now. And still can not get it to work, I think the reason why is because I have a limited understanding of inheritance.


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
#include <iostream>
#include <iomanip>
#include <string>
using namespace std;

class Student
{
protected:
	
	int social;
	string name;
	string *NamePtr;
public:
	Student (); // Default constructor
	Student(int ssn, string StudentName);
	void setSSN(int);
	int getSSN();
	void setName(string);
    string getName();
	virtual double calcGPA();
	virtual void setQualityPts(int) =0;
	
};

Student ::Student()
{
}
Student::Student(int ssn, string  StudentName)
{
	
	social = ssn;
	name = StudentName;
	
	return;
}

void Student::setSSN(int SSN)
{
		social = SSN;
		return;
}

int Student::getSSN()
{
	return (social);
}

void Student::setName(string name2)
{
		name = name2;
		return;
}

string Student::getName() 
{	

return name;
		
}

double Student::calcGPA()
{
	cout << "Student calcGPA" << endl;
	return (0);
}

class GradStudent : public Student 
{
private:
	char Status;
public:
	GradStudent (char = 'A');
	virtual double calcGPA();
	void SetStatus(char);
};

GradStudent::GradStudent(char grade)
{
	Status = grade;
}

double GradStudent::calcGPA()
{
	double GPA = 0.0;
	if (Status == 'A' || Status == 'a')
		GPA = 4.0;
	cout << "GradStudent calcGPA";
	return (GPA);
}

void GradStudent::SetStatus (char stat)
{
	Status = stat;
	return;
}

class UnderGradStudent : public Student
{
private:
	double CreditHrs;
	double QualityPts;
public:
	UnderGradStudent ( double = 0, double = 0);
	virtual double calcGPA ();
	void setCreditHrs(int);
	virtual void setQualityPts(int) =0;
};

UnderGradStudent::UnderGradStudent(double Hours, double Points)
{
	CreditHrs = Hours;
	QualityPts = Points;
	if (Hours < 0)
		CreditHrs = 0;
	if (Points < 0)
		QualityPts = 0;
}

double UnderGradStudent::calcGPA ()
{
	double GPA = 0.0;
	if (CreditHrs > 0)
		GPA = QualityPts / CreditHrs;
	cout << "UnderGraduateStudent calcGPA";
	return (GPA);
}

void UnderGradStudent::setCreditHrs (int C)
{
	CreditHrs = C;
	if (C < 0)
		CreditHrs = 0;
	return;
}

void UnderGradStudent::setQualityPts (int Q)
{
	QualityPts = Q;
	if (Q < 0)
		QualityPts = 0;
	return;
}


int main()
{
	char ans = 'n', grade, Status, ug;
	string StudentName;
	int StudentSSN;
	int code, CreditHrs, points;

	
	Student* a = NULL;

	do
	{
		cout << "Enter 1 for undergrad.\nEnter 2 for grad." << endl << "-->";
		cin >> code;
	
		if (code == 1)
		{
			  
			  a = new UnderGradStudent;
			  

			cin.ignore();
			cout << "Enter Student name: ";
			getline(cin, StudentName);
			a->setName (StudentName);
			cout << "Enter Student SSN: ";
			cin >> StudentSSN;
			a->setSSN(StudentSSN);

			cout << "Enter credit hours: ";
			cin >> CreditHrs;
			a->setCreditHrs(CreditHrs);

			cout << "Enter quality points: ";
			cin >> points;
			a->setQualityPts(points);
			cout << endl;
			
			
		}

		else if (code == 2)
		{
			a = new GradStudent;
			
			cin.ignore();
			cout << "Enter Student name: ";
			getline(cin, StudentName);
			a->setName(StudentName);
			
			
			cout << "Enter Student SSN: ";
			cin >> StudentSSN;
			a->setSSN(StudentSSN);

			cout << "Enter Status: ";
			cin >> Status;
			a->setStatus(Status);
			cout << endl;
		}

		
		

		
		cout <<  "\nName: " << a->getName() << " SSN: " << a->getSSN() << " GPA: "
			<< setiosflags(ios::fixed | ios::showpoint) << setprecision(2) << a->calcGPA() << endl;

		
		delete a;
		a = NULL;

		cout << "Continue?: ";
		cin >> ans;
		cin.ignore();
		cout << endl;
	}
	while (toupper(ans) == 'Y');

	

	return 0;
}
I showed you how to fix this in my previous post.
I suggest you go back and reread it, especially the part about line 171.

Dev C++ kept displaying error for that line of code.
Then post your current code and the list of errors you're getting.
The code I posted compiled fine for me.

From the code above, you're assigning new UnderGradStudent to a. That's legal, but the compiler does an automatic downcast to type Student since that is the type of a. Once that happens, members unique to UnderGradStudent are no longer visible since they are not part of Student. This why I created a pointer called ug of type UnderGradStudent *. By doing that, all members of UnderGradStudent are available. I then did a downcast of ug to Student * by assigning it to a. Of course, the same applies to GradStudent.

By downcasting UnderGradStudent or GradStudent to Student *, that allows your cout at line 204 (original snippet) to reference members of the common base class.

Last edited on





Thank You, for all of you help.


Line 164 E:\InheritStudent.cpp [Error] invalid new-expression of abstract class type UnderGradStudent'
Line 98 E:\InheritStudent.cpp [Note] because the following virtual functions are pure within 'UnderGradStudent':
Line 137 E:\InheritStudent.cpp [Note] virtual void UnderGradStudent::setQualityPts(int)
Line 190 E:\InheritStudent.cpp [Error] invalid new-expression of abstract class type 'GradStudent'
Line 68 E:\InheritStudent.cpp [Note] because the following virtual functions are pure within 'GradStudent':
Line 22 E:\InheritStudent.cpp [Note] virtual void Student::setQualityPts(int)
Line 204 E:\InheritStudent.cpp [Error] 'class Student' has no member named 'setStatus'













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
#include <iostream>n >
#include <iomanip>
#include <string>
using namespace std;

class Student
{
protected:
	
	int social;
	string name;
	
public:
	Student (); // Default constructor
	Student(int ssn, string StudentName);
	void setSSN(int);
	int getSSN();
	void setName(string);
    string getName();
	virtual double calcGPA();
	virtual void setQualityPts(int) =0;
	
};

Student ::Student()
{
}
Student::Student(int ssn, string  StudentName)
{
	
	social = ssn;
	name = StudentName;
	
	return;
}

void Student::setSSN(int SSN)
{
		social = SSN;
		return;
}

int Student::getSSN()
{
	return (social);
}

void Student::setName(string name2)
{
		name = name2;
		return;
}

string Student::getName() 
{	

return name;
		
}

double Student::calcGPA()
{
	cout << "Student calcGPA" << endl;
	return (0);
}

class GradStudent : public Student 
{
private:
	char Status;
public:
	GradStudent (char = 'A');
	virtual double calcGPA();
	void SetStatus(char);
};

GradStudent::GradStudent(char grade)
{
	Status = grade;
}

double GradStudent::calcGPA()
{
	double GPA = 0.0;
	if (Status == 'A' || Status == 'a')
		GPA = 4.0;
	cout << "GradStudent calcGPA";
	return (GPA);
}

void GradStudent::SetStatus (char stat)
{
	Status = stat;
	return;
}

class UnderGradStudent : public Student
{
private:
	double CreditHrs;
	double QualityPts;
public:
	UnderGradStudent ( double = 0, double = 0);
	virtual double calcGPA ();
	void setCreditHrs(int);
	virtual void setQualityPts(int) =0;
};

UnderGradStudent::UnderGradStudent(double Hours, double Points)
{
	CreditHrs = Hours;
	QualityPts = Points;
	if (Hours < 0)
		CreditHrs = 0;
	if (Points < 0)
		QualityPts = 0;
}

double UnderGradStudent::calcGPA ()
{
	double GPA = 0.0;
	if (CreditHrs > 0)
		GPA = QualityPts / CreditHrs;
	cout << "UnderGraduateStudent calcGPA";
	return (GPA);
}

void UnderGradStudent::setCreditHrs (int C)
{
	CreditHrs = C;
	if (C < 0)
		CreditHrs = 0;
	return;
}

void UnderGradStudent::setQualityPts (int Q)
{
	QualityPts = Q;
	if (Q < 0)
		QualityPts = 0;
	return;
}


int main()
{
	char ans = 'n', grade, Status;
	string StudentName;
	int StudentSSN;
	int code, CreditHrs, points;

	
	Student* a = NULL;

	do
	{
		cout << "Enter 1 for undergrad.\nEnter 2 for grad." << endl << "-->";
		cin >> code;
	
		if (code == 1)
		{
			  UnderGradStudent* ug;
			  ug = new UnderGradStudent();
			  a = ug;
			  

			cin.ignore();
			cout << "Enter Student name: ";
			getline(cin, StudentName);
			a->setName (StudentName);
			cout << "Enter Student SSN: ";
			cin >> StudentSSN;
			a->setSSN(StudentSSN);

			cout << "Enter credit hours: ";
			cin >> CreditHrs;
			ug->setCreditHrs(CreditHrs);

			cout << "Enter quality points: ";
			cin >> points;
			a->setQualityPts(points);
			cout << endl;
			
			
		}

		else if (code == 2)
		{
			a = new GradStudent;
			
			cin.ignore();
			cout << "Enter Student name: ";
			getline(cin, StudentName);
			a->setName(StudentName);
			
			
			cout << "Enter Student SSN: ";
			ci> StudentSSN;
			a->setSSN(StudentSSN);

			cout << "Enter Status: ";
			cin >> Status;
			a->setStatus(Status);
			cout << endl;
		}

		
		

		
		cout <<  "\nName: " << a->getName() << " SSN: " << a->getSSN() << " GPA: "
			<< setiosflags(ios::fixed | ios::showpoint) << setprecision(2) << a->calcGPA() << endl;

		
		delete a;
		a = NULL;

		cout << "Continue?: ";
		cin >> ans;
		cin.ignore();
		cout << endl;
	}
	while (toupper(ans) == 'Y');

	

	return 0;
} 
Line 164 E:\InheritStudent.cpp [Error] invalid new-expression of abstract class type UnderGradStudent'

Line 106: Why did you make this a pure virtual function? Pure virtual implies you have a class derived from UnderGradStudent and the function is implemented in the derived class. You have no class derived from UnderGradStudent. Remove the virtual and the =0.

Line 204 E:\InheritStudent.cpp [Error] 'class Student' has no member named 'setStatus'

Line 189: What I explained about UnderGradStudent also applies to GradStudent.


This program is very frustrating. I used the code below and DEV C++ keeps giving me errors.


1
2
3
4
5
UnderGradStudent *  ug;
    ug = new UnderGradStudent;
    a = ug;
...
ug->setCreditHrs(CreditHrs);
What errors? Not a mind reader here.

Line 1: You have a typo.

AbstractionAnon wrote:
Line 106: Why did you make this a pure virtual function? Pure virtual implies you have a class derived from UnderGradStudent and the function is implemented in the derived class. You have no class derived from UnderGradStudent. Remove the virtual and the =0.


Line 21: This line does not belong in Student. Delete it.

AbstractionAnon wrote:
Line 189: What I explained about UnderGradStudent also applies to GradStudent.


I'm repeating what I told you before. I don't know how I can make it any more clear.

Line 203: Your capitalization is incorrect. Should be SetStatus.

This compiles without error:
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
#include <iostream>
#include <iomanip>
#include <string>
using namespace std;

class Student
{
protected:
	
	int social;
	string name;
	
public:
	Student (); // Default constructor
	Student(int ssn, string StudentName);
	void setSSN(int);
	int getSSN();
	void setName(string);
    string getName();
	virtual double calcGPA();

	
};

Student ::Student()
{
}
Student::Student(int ssn, string  StudentName)
{
	
	social = ssn;
	name = StudentName;
	
	return;
}

void Student::setSSN(int SSN)
{
		social = SSN;
		return;
}

int Student::getSSN()
{
	return (social);
}

void Student::setName(string name2)
{
		name = name2;
		return;
}

string Student::getName() 
{	

return name;
		
}

double Student::calcGPA()
{
	cout << "Student calcGPA" << endl;
	return (0);
}

class GradStudent : public Student 
{
private:
	char Status;
public:
	GradStudent (char = 'A');
	virtual double calcGPA();
	void SetStatus(char);
};

GradStudent::GradStudent(char grade)
{
	Status = grade;
}

double GradStudent::calcGPA()
{
	double GPA = 0.0;
	if (Status == 'A' || Status == 'a')
		GPA = 4.0;
	cout << "GradStudent calcGPA";
	return (GPA);
}

void GradStudent::SetStatus (char stat)
{
	Status = stat;
	return;
}

class UnderGradStudent : public Student
{
private:
	double CreditHrs;
	double QualityPts;
public:
	UnderGradStudent ( double = 0, double = 0);
	virtual double calcGPA ();
	void setCreditHrs(int);
	void setQualityPts(int);
};

UnderGradStudent::UnderGradStudent(double Hours, double Points)
{
	CreditHrs = Hours;
	QualityPts = Points;
	if (Hours < 0)
		CreditHrs = 0;
	if (Points < 0)
		QualityPts = 0;
}

double UnderGradStudent::calcGPA ()
{
	double GPA = 0.0;
	if (CreditHrs > 0)
		GPA = QualityPts / CreditHrs;
	cout << "UnderGraduateStudent calcGPA";
	return (GPA);
}

void UnderGradStudent::setCreditHrs (int C)
{
	CreditHrs = C;
	if (C < 0)
		CreditHrs = 0;
	return;
}

void UnderGradStudent::setQualityPts (int Q)
{
	QualityPts = Q;
	if (Q < 0)
		QualityPts = 0;
	return;
}


int main()
{
	char ans = 'n', Status;
	string StudentName;
	int StudentSSN;
	int code, CreditHrs, points;

	
	Student* a = NULL;

	do
	{
		cout << "Enter 1 for undergrad.\nEnter 2 for grad." << endl << "-->";
		cin >> code;
	
		if (code == 1)
		{
			  UnderGradStudent* ug;
			  ug = new UnderGradStudent;
			  a = ug;
			  

			cin.ignore();
			cout << "Enter Student name: ";
			getline(cin, StudentName);
			ug->setName (StudentName);
			cout << "Enter Student SSN: ";
			cin >> StudentSSN;
			ug->setSSN(StudentSSN);

			cout << "Enter credit hours: ";
			cin >> CreditHrs;
			ug->setCreditHrs(CreditHrs);

			cout << "Enter quality points: ";
			cin >> points;
			ug->setQualityPts(points);
			cout << endl;
			
			
		}

		else if (code == 2)
		{   GradStudent * gs;
             
            gs = new GradStudent;
			a = gs;
			
			cin.ignore();
			cout << "Enter Student name: ";
			getline(cin, StudentName);
			gs->setName(StudentName);
			
			
			cout << "Enter Student SSN: ";
			cin >> StudentSSN;
			gs->setSSN(StudentSSN);

			cout << "Enter Status: ";
			cin >> Status;
			gs->SetStatus(Status);
			cout << endl;
		}

		
		

		
		cout <<  "\nName: " << a->getName() << " SSN: " << a->getSSN() << " GPA: "
			<< setiosflags(ios::fixed | ios::showpoint) << setprecision(2) << a->calcGPA() << endl;

		
		delete a;
		a = NULL;

		cout << "Continue?: ";
		cin >> ans;
		cin.ignore();
		cout << endl;
	}
	while (toupper(ans) == 'Y');

	

	return 0;
} 

Last edited on
Thank You, for all of your assistance and patience.
Topic archived. No new replies allowed.