I have to implement a set of classes using inheritance and composition, the clas
ID: 3729992 • Letter: I
Question
I have to implement a set of classes using inheritance and composition, the classes can be seen below. I know this class is pretty big so if I could just get a pseudo code of how to approach each class I’d be very grateful.
class person
{
public :
person ();
person ( string , string , string );
string getFirst () const ;
string getLast () const ;
string getId () const ;
private :
string first ;
string last ;
string id ;
};
class student
{
public :
static const int CREDITS_NEEDED = 15;
student ();
student ( string , string , string );
person getPersonInfo () const ;
unsigned int getCredits () const ;
double getGpa () const ;
void addCredits ( unsigned int , double );
bool graduated () const ;
private :
person info ;
unsigned int credits ;
double gpa ;
};
1
class undergrad : student
{
public :
undergrad ();
undergrad ( string , string , string );
string getFirst () const ;
string getLast () const ;
unsigned int getCredits () const ;
double getGpa () const ;
bool searchById ( string ) const ;
bool searchByName ( string ) const ;
void completedClass ( double );
bool graduated () const ;
};
class grad : student
{
public :
grad ();
grad ( string , string , string , string );
string getFirst () const ;
string getLast () const ;
unsigned int getCredits () const ;
double getGpa () const ;
bool searchById ( string ) const ;
bool searchByName ( string ) const ;
void completedClass ( double );
bool graduated () const ;
void completedThesis ();
bool hasDefended () const ;
private :
string thesis ;
bool defended ;
};
class phd : student
{
public :
phd ();
phd ( string , string , string , string );
string getFirst () const ;
string getLast () const ;
unsigned int getCredits () const ;
double getGpa () const ;
bool searchById ( string ) const ;
bool searchByName ( string ) const ;
void completedClass ( double );
bool graduated () const ;
void completedDissertation ();
bool hasDefended () const ;
private :
string dissertation ;
bool defended ;
};
As always, you cannot modify the class, you need to implement those functions as instructed and you cannot change the inheritance type, you can have extra helper (non member) functions in main or in the class implementation file, as always please comment your code (as well as the member functions) and use constants rather than literals as needed
Example Run
Enter Student ID or full name or q to quit : 3451
Star , Patrick
GPA : 3.4 Credits : 3
Graduated : No
Has the student completed a course ? ( Yes or No ): yes
Enter updated GPA : 3.6
Enter Student ID or full name or q to quit : patrick star
Star , Patrick
GPA : 3.6 Credits : 6
Graduated : No
Has the student completed a course ? ( Yes or No ): yes
Enter updated GPA : 3.3
Here is some of the data used
John Smith 1234 U
Patrick Star 3451 U
Not Sure 1111 P Smartest_Man_In_The_World
Colonel Mustard 2316 P A_Game_Of_Clue
Explanation / Answer
Here are three classes that i have defined
Let us consider these three classes
class person
{
public :
person (); //initialize first, last and id with empty string
person ( string , string , string );//first = param1, last = param2, id = param3
string getFirst () const ;//return first
string getLast () const ;//return last
string getId () const ;//retrun id
private :
string first ;
string last ;
string id ;
};
class student
{
public :
static const int CREDITS_NEEDED = 15;
student (); //contructors are used to initialize. so initialize info to null or person(), gpa and credits to 0
student ( string , string , string ); //make info = person(param1, param2, param3), gpa and credits to 0
person getPersonInfo () const ; //return info
unsigned int getCredits () const ; //return credits
double getGpa () const ; //return gpa
void addCredits ( unsigned int , double ); //credit = credit + param1; gpa = gpa + prarm2
bool graduated () const ; //return credit >= 15
private :
person info ;
unsigned int credits ;
double gpa ;
};
and
class grad : student
{
public :
grad ();// same as above, i.e. initialize with nulls and 0s
grad ( string , string , string , string ); //call student(param1, param2, param3), thesis = param4
string getFirst () const ; //return getPersonInfo().getFirst();
string getLast () const ;//return getPersonInfo().getLast();
unsigned int getCredits () const ; // remove, inherited from student
double getGpa () const ;// remove, inherited from student
bool searchById ( string ) const ; // Bussiness logic
bool searchByName ( string ) const ;// Bussiness logic
void completedClass ( double ); // Bussiness logic
bool graduated () const ;// Bussiness logic
void completedThesis ();// Bussiness logic
bool hasDefended () const ; //return defended
private :
string thesis ;
bool defended ;
};
Where ever you find bussiness logic, it indicates that the logic to implememt is depending on the usecase, that you will better know.
Advice: Where ever you find a function name starting with get, you are suppose to return the value of parameter indicated just after 'get'
for example getCreditwill retrurn credit
I hope with this information you can proceed with all the codes.