Question: Can you help explain the output of this program ( a ) Here is a Person class with a Student subclass and a Professor subclass:

Can you help explain the output of this program

( a ) Here is a Person class with a Student subclass and a Professor subclass:

What is the output of the following program?

class Person {

public:

person ( const string& s = 0 ) : name ( s ) { }

void print ( ) const { cout << "My name is "<< name << endl; }

protected:

string name;

};

class Student : public Person {

public:

Student ( const string& s = 0, const float& g = 0 ) : Person ( s ), gpa ( g ) { }

void print ( ) const { Person :: print ( );

cout << "and my GPA is " << gpa << endl; }

private:

float gpa;

};

class Professor : public Person {

public:

Professor ( const string& s = 0, const unsigned& n = 0 ) : Person ( s ), pubs ( n ) { }

void print ( ) const { Person :: print ( );

cout << "and I have " << pubs << " publications " << endl; }

private:

unsigned pubs;

};

int main ( )

{

Person* p;

Person x ( "Bob" ); p = &x; p->print ( );

Student y ( "Tom", 3.47 ); p = &y; p->print ( );

Professor z ( "Ann", 7); p = &z; p->print ( );

return 0;

}

b) if you replace The print ( ) function in the Person class with the following statement, what will be the output of this program? explain

virtual void print ( ) const { cout << "My name is " << name << endl; }

Step by Step Solution

There are 3 Steps involved in it

1 Expert Approved Answer
Step: 1 Unlock blur-text-image
Question Has Been Solved by an Expert!

Get step-by-step solutions from verified subject matter experts

Step: 2 Unlock
Step: 3 Unlock

Students Have Also Explored These Related Databases Questions!