C++ Multiple Inheritance

I am currently stuck and I cannot get the setScore(numericScore) to recognize on PassFailExam.cpp . Any help toward a solution will be much appreciated.

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
//GradedActivity.cpp
#include "GradedActivity.h"

GradedActivity::GradedActivity()
{
	this->score = 0.0;
}

GradedActivity::GradedActivity(double testScore)
{
	this->score = testScore;
}

GradedActivity::~GradedActivity()
{

}

void GradedActivity::setScore(double testScore)
{
	this->score = testScore;
}

double GradedActivity::getScore()
{
	return this->score;
}

char GradedActivity::getLetterGrade(double testScore)
{
	if (testScore >= 90.0)
		return 'A';
	else if (testScore <= 89.9 && testScore >= 80.0)
		return 'B';
	else if (testScore <= 79.9 && testScore >= 70.0)
		return 'C';
	else if (testScore <= 69.9 && testScore >= 60.0)
		return 'D';
	else if (testScore < 60)
		return 'F';
	else
		return 1;
}
//GradedActivity.h
#ifndef GRADEDACTIVITY_H
#define GRADEDACTIVITY_H
#include <iostream>
#include <iomanip>
#include <string>

class GradedActivity {
protected:
	double score;
public:
	GradedActivity();
	GradedActivity(double testScore);
	~GradedActivity();
	void setScore(double testScore);
	double getScore();
	char getLetterGrade(double testScore);
};

#endif
//PassFailActivity.h
#ifndef PASSFAILACTIVITY_H
#define PASSFAILACTIVITY_H
#include <iostream>
#include <iomanip>
#include <string>
#include "GradedActivity.h"

class PassFailActivity : GradedActivity {
protected:
	double minPassingScore;
	GradedActivity testScore;
public:
	PassFailActivity();
	PassFailActivity(double mps);
	~PassFailActivity();
	void setMinPassingScore(double mps);
	double getMinPassingScore(double testScore);
	char getLetterGrade(double testScore);
};

#endif
//PassFailActivity.cpp
#include "PassFailActivity.h"

PassFailActivity::PassFailActivity()
{

}

PassFailActivity::PassFailActivity(double mps)
{
	this->minPassingScore = mps;
}

PassFailActivity::~PassFailActivity()
{

}

void PassFailActivity::setMinPassingScore(double mps)
{
	this->minPassingScore = mps;
}

double PassFailActivity::getMinPassingScore(double testScore)
{
	if (testScore >= this->minPassingScore)
		return 'P';
	else if (testScore < this->minPassingScore)
		return 'F';
}

char PassFailActivity::getLetterGrade(double testScore)
{
	if (testScore >= 90.0)
		return 'A';
	else if (testScore <= 89.9 && testScore >= 80.0)
		return 'B';
	else if (testScore <= 79.9 && testScore >= 70.0)
		return 'C';
	else if (testScore <= 69.9 && testScore >= 60.0)
		return 'D';
	else if (testScore < 60)
		return 'F';
	else
		return 1;
}
//PassFailExam.h
#ifndef PASSFAILEXAM_H
#define PASSFAILEXAM_H
#include "PassFailActivity.h"

class PassFailExam : PassFailActivity {
private:
	int numQuestions;
	double pointsEach;
	int numMissed;
public:
	PassFailExam();
	PassFailExam(int questions, int missed, double mps);
	~PassFailExam();
	void set(int questions, int missed);
	double getNumQuestions();
	double getPointsEach();
	double getNumMissed();
};

#endif
//PassFailExam.cpp
#include "PassFailExam.h"

PassFailExam::PassFailExam()
{
	this->numQuestions = 0;
	this->pointsEach = 0.0;
	this->numMissed = 0;
}

PassFailExam::PassFailExam(int questions, int missed, double mps)
	: PassFailActivity(mps)
{
	set(questions, missed);
}

PassFailExam::~PassFailExam()
{

}

void PassFailExam::set(int questions, int missed)
{
	double numericScore;

	numQuestions = questions;
	numMissed = missed;

	pointsEach = 100.0 / numQuestions;

	numericScore = 100.0 - (missed * pointsEach);

	setScore(numericScore);
}

double PassFailExam::getNumQuestions()
{

}

double PassFailExam::getPointsEach()
{

}

double PassFailExam::getNumMissed()
{

}
//main.cpp
#include <iostream>
#include <iomanip>
#include "PassFailExam.h"
using namespace std;

int main()
{
	int questions;
	int missed;
	double minPassing;

	GradedActivity exam_1(88.0);

	cout << "the student's grade on Exam #1 is "
		<< exam_1.getLettergrade() << endl << endl
		<< "---------------------------\n\n";

	cout << "How many questions are on the exam? ";
	cin >> questions;

	cout << "How many questions did the student miss? ";
	cin >> missed;

	cout << "Enter the minimum passing score for this test: ";
	cin >> minPassing;

	PassFailExam exam_2(questions, missed, minPassing);

	cout << fixed << setprecision(1);

	cout << "\nEach question counts "
		<< exam_2.getPointsEach() << "points.\n";

		cout << "The minimum passing score is "
		<< exam_2.getMinPassingScore() << endl;

	cout << "The student's exam score is "
		<< exam_2.getScore() << endl;

	cout << "The student's grade on Exam #2 is "
		<< exam_2.getLetterGrade() << endl;
	
	return 0;
}
Careful with your use of terminology -- you are not using multiple inheritance; this is when one class has two or more base classes, e.g.

1
2
class AmphibiousVehicle : public Car, public Boat {
    // etc. 


(prob. not such a hot example, but you get the point?!)

What you have is multiple levels of single inheritance.

Anyway...

Your main problem is that you are inadvertently using private inheritance. You should be using public inheritance. e.g.

1
2
class PassFailActivity : public GradedActivity { // public before base class name
    // etc 


Almost all inheritance should be public -- private and protected inheritance have their place, but not for this kind of situation.

Changing all your derived classes to use public inheritance fixes the problem with setScore() but exposes several other errors:

1. getLettergrade doesn't exist
2. PassFailActivity::getMinPassingScore doesn't take zero arguments
3. PassFailExam::getNumQuestions isn't returning a value
4. ...

But these are you problems!

Andy

PS It is better form to use the constructor initializer list to initialize member variables:

1
2
3
4
5
6
7
PassFailExam::PassFailExam() : numQuestions(0), pointsEach(0.0), numMissed(0) // init here
{
	// rather than here!
	//this->numQuestions = 0;
        //this->pointsEach = 0.0;
	//this->numMissed = 0;
}


Not such a big deal for built-in types, but can be important for class members.
Last edited on
Topic archived. No new replies allowed.