Academic Integrity: tutoring, explanations, and feedback — we don’t complete graded work or submit on a student’s behalf.

Can someone help me with this practice exam please? For questions 1 - 6 consider

ID: 3699534 • Letter: C

Question

Can someone help me with this practice exam please?

For questions 1 - 6 consider the following code:

enum Level {Freshman, Sophomore, Junior, Senior, Graduate};

class Student

{

double _age;

double _GPA;

unsigned _total_units;

public:

Student ( ) { };

Student ( unsigned u ) { _total_units = u; };

double GPA ( )const { return ( _GPA ); };

double& GPA ( ) { return ( _GPA ); };

bool Passing ( )const { return ( _GPA >= 2.0 ); };

virtual unsigned TotalUnits ( )const { return ( _total_units ); };

virtual void setUnits ( unsigned u ) { _total_units = u; };

virtual Level Grade ( )const = 0;

};

class UndergradStudent : public Student

{

Level _grade_level;

public:

UndergradStudent ( );

UndergradStudent ( unsigned u ):Student(u) {};

Level Grade ( )const { return ( _grade_level ); };

};

class GradStudent : public Student

{

unsigned _grad_units;

public:

GradStudent ( );

GradStudent ( unsigned gu ) { _grad_units = gu; };

unsigned TotalUnits ( )const

            { return (_grad_units + Student::TotalUnits());};

void setUnits ( unsigned u ) { _grad_units = u; };

Level Grade ( )const { return ( Graduate ); };

bool Passing ( )const { return ( GPA() >= 3.0 ); };

};

const unsigned NUMBER = 3;

int main ( ) {

UndergradStudent UG1(10);

GradStudent G1(15);

GradStudent G2(10);

GradStudent* PG2 = &G2;

Student* AllStudents [NUMBER] ={&UG1, &G1, &G2};

for ( int i=0; i<NUMBER; i++)

{

    AllStudents[i]->GPA()= 2.5;

    cout << AllStudents[i]->Passing()<<"   ";

}

cout<<PG2->Passing();

return 0;

}

1- What do the derived classes inherit from Student::setUnits()? How do they use what they inherit?

2- What does the function Student::Grade() say to the derived classes and about the base class?

3- What are the differences between the two Student::GPA functions and when/why would you use each of them? What do the derived classes inherit from them?

4- What will the program output?

5- Resolve the scope for the following (I have done the first one)

AllStudents[0]->Passing()     __ex: Student::Passing()_____

AllStudents[1]->Passing()     _____________________________

Allstudents[2]->Passing()     _____________________________

PG2->Passing()                _____________________________

6- Explain why/how each function in the previous problem was chosen to be called.

Explanation / Answer

The answers are following:

1) The derived classes inherit _total_units from base class Student::setUnits(). The method is virtual in base class. So we can achieve run time ingeritance means method overriding.

How to use:

Student *st;

UndergradStudent us;

GradStudent gs;

us.setUnits();                      // Access _total_units in undergradStudent class.

st = &gs;

st->setUnits();                  // Access _total_units in gradStudent class.

2) Student::Grade() is pure virtual function in Student class. It means we have to define body of Grade() in derived class. UndergradStudent and GradStudent class inherits Student class so we need to define body in derived class. In derived class it returns grad_level which is defined in enum Level.

3) double GPA ( )const { return ( _GPA ); }: This means method is constant. When you add the const keyword to a method the this pointer will essentially become const, and you can therefore not change any member data like _GPA . This method we can call it by constant object only.

double& GPA ( ) { return ( _GPA ); } : It basically means the returned value is an alias to whatever you returned from the function means it returns alias of _GPA.

Derived classes UndergradStudent and GradStudent can inherits _GPA data with constant as well as non constant based on class object type. If we create constant object of derived class and call GPA() then it will return constant _GPA. If we create non constant object of derived class and call GPA() then it will return _GPA of Base call.

4) Program Output:

AllStudents[i]->Passing() : Prints the _GPA of GradStudent as well as UndergradStudent whose _GPA is >= 2.0

PG2->Passing() : Prints the _GPA of Graduate student whose _GPA > = 3.0

5)

AllStudents[0]->Passing()    :- Student::Passing()

AllStudents[1]->Passing()    : - Student::Passing()

Allstudents[2]->Passing()    :- Student::Passing()

PG2->Passing()                    :- GradStudent::Passing()

Hire Me For All Your Tutoring Needs
Integrity-first tutoring: clear explanations, guidance, and feedback.
Drop an Email at
drjack9650@gmail.com
Chat Now And Get Quote