Looking at your original code, it's not clear whether Quiz was meant to be Exam: at the moment Quiz inherits from question.
It's also not clear whether a Student should keep track of multiple exams,given there are multiple question types, and Student is supposed to have
addPointsPossible
, a function which implies addition.
But I think I see what your saying since it is already public I can retrieve it with out inheritance? |
Yes.
A key thing about OOP is the concept of an interface, that is a series of public functions which return the values of member variables. Other objects can use these functions to acquire the data that they need. Contrary to beginners belief, there is no need to put everything into one giant inheritance tree.
Note that one may not need access to
all the class members, and one shouldn't routinely provide get / set functions for every member. The public interface to Student could be minimal, just:
void PrintExamResults(const Exam& TheExam);
The functions
addPointsPossible
,
addPointsScored
,
getPointsPossible
, and
getPointsScored
could all be private member functions that use the public interface of an Exam object to get those values.
I was thinking that it would go out of scope so I would have to inherit to prevent it, is that wrong? |
Yes IMO that is wrong. Simplistically, in main you should have an instances of all the object types: you can use them to call whatever public functions their classes contain. In reality though, a class could
have instance of another class inside it. Be careful to initialise them via the constructor.
That brings me to another point, your classes don't have constructors. The purpose of these is to initialise their member variables.
TheIdeasMan wrote: |
---|
What other thing can a student have? It relates to where they learn. |
Maybe my vision was too complicated for this assignment: I am used to thinking about reuse of objects, so I tend to have more classes rather than less. I looks like the assignment is only asking for question and it's derived classes, Exam and Student.