Question
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
Get Instant Access to Expert-Tailored Solutions
See step-by-step solutions with expert insights and AI powered tools for academic success
Step: 2
Step: 3
Ace Your Homework with AI
Get the answers you need in no time with our AI-driven, step-by-step assistance
Get Started