I have a homework problem that asks:
Desgin a derived class GraduateStudent from base class Student. It adds a data member named Advisor to store the name of student’s thesis advisor. Provide constructor, destructor, accessor, and modifier functions. This class overrides the year function to returns one of two strings (“Masters” or “PhD”) depending on the number of hours completed, using using the chart below.
Hours Year
<=30 Masters
(greater than) 30 PhD
I’ve written:
using namespace std;
class Student
{
public:
Student( string s = "", int h = 0);
~Student();
string getName() const;
int getHours() const;
void setName(string s);
void setHours (int h);
string year () const;
private:
string name;
int hours;
};
class GraduateStudent: private Student
{
public:
GraduateStudent(string s = "", int h=0, string advisor=""); //constructor
~GraduateStudent(); //destructor
string getAdvisor();
void setAdvisor(string advisor);
//Class overrides??
private:
string Advisor;
}
The class student was given, I’ve added the Graduate Student derived class.
My question is first if I have set up the derived class properly. My second question is how can I override the year function without if statements? I tried to use those statements but the editor gave me errors, I suspect since its a header file.
The student class you were “given” is not really written correctly to properly allow for overriding of the year function. A general use case scenario (where you hold student objects in a container won’t work properly).
So, to fix (properly):
Also, why do you think you are required to implement the
year()function without usingifstatements? That would be the appropriate thing to do in this case.