Answered step by step
Verified Expert Solution
Link Copied!

Question

1 Approved Answer

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

Can you help explain the output of this program please

( 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

Step: 1

blur-text-image

Get Instant Access to Expert-Tailored Solutions

See step-by-step solutions with expert insights and AI powered tools for academic success

Step: 2

blur-text-image

Step: 3

blur-text-image

Ace Your Homework with AI

Get the answers you need in no time with our AI-driven, step-by-step assistance

Get Started

Recommended Textbook for

Database And Expert Systems Applications 31st International Conference Dexa 2020 Bratislava Slovakia September 14 17 2020 Proceedings Part 1 Lncs 12391

Authors: Sven Hartmann ,Josef Kung ,Gabriele Kotsis ,A Min Tjoa ,Ismail Khalil

1st Edition

303059002X, 978-3030590024

More Books

Students also viewed these Databases questions

Question

5. Our efficiency focus eliminates free time for fresh thinking.

Answered: 1 week ago