Inheritance Question

Hello Community,

I currently try to solve a programming problem involving inheritance of multiple classes. Here is part of the problem statement:

Write a class named CourseGrades. The class should have a member named grades that is an array of GradedActivity pointers. The grades array should have four elements, one for each of the assignments previously described.


Here is my class diagram:

https://ibb.co/eUO95c

My question/problem, if you will, revolves around the question what to do with CourseGrades class.

I tried to come up with arguments for and against the decision to have that class to be derived from the base class, which is GradedActivity. I could not find any argument, as a CourseGrade is no GradedActivity.

Then I thought about the possibility of making the CourseGrades a base class, which does not seem to fit the problem statement. With my still very limited knowledge I fail to see, how CourseGrades could become the base class AND allow for the possibility to have an array of pointers to GradedActivity objects.

So, the question, in short, is: Should CourseGrades be its own class, or should it be part of the whole, and if so, how? Or, rather, where to place it? Any help, links to topics I might find helpful (but lacking the correct vocabulary to look it up myself), is highly appreciated!

Here is the CourseGrades class file:

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
#ifndef COURSE_GRADES_H_
#define COURSE_GRADES_H_

#include "GradedActivity.h"
#include "PassFailExam.h"
#include "FinalExam.h"
#include "Essay.h"

#include <array>

const int NUM_OBJ = 4;

class CourseGrades
{
	private:
		std::array<GradedActivity *, NUM_OBJ> grades;
		
	public:	
		CourseGrades() : grades{}
		{
		}

		void setLab(GradedActivity *);
		void setPassFailExam(PassFailExam *);
		void setEssay(Essay *);
		void setFinalExam(FinalExam *);

		void print() const;
};

#endif 
Last edited on
Why do you want to parent it with the other classes? There's nothing in your problem text that states so.
You are correct, there is no indication of it that I should parent it. But it isn't about whether I want to parent it, which I don't want to do. The question still is relevant in that, iff a CourseGrade is not a GradedActivity, thus it should not be a derived class, the only other possibility would have to be that it becomes a parent. (Turning in circles here, sorry ...)

So, maybe the questions should be: "Is there any indication that CourseGrades should be a derived class of the GradedActivity class", and why?"

and

"Is there some connection that I fail to see, that would make CourseGrades fit into the inheritance chain?"

Last edited on
When you ask yourself whether to inherite from a particular class or not, then the answer is usually no.

A composition is in most cases more flexible. Thus you should have a good reason to inherit.

The reason why you should inherit usually starts with the keyword virtual. I.e. you want to extent/modify the behavior of a base class by overriding virtual function(s). At least the destructor should be virtual.

In other words: If you neither want to extent nor modify the behavior of the base class don't inherit.
coder777, first of all thank you! This is the answer I was looking for.

There really is no good reason for having the CourseGrades class inherit anything from the GradedActivity class. There is also no plan to extent or modify the base class. Lastly, there is the fact that CourseGrades is no GradedActivity. Three strikes against inheritance. So, the decision is to choose composition in this case.
TheIdeasMan thank you for sharing this! I will certainly try to learn, internalize, and adhere to it.
Topic archived. No new replies allowed.