Question: Write the output of given code segments and explain it logically. 1 . #include using namespace std; class Base { public: virtual void show() {

Write the output of given code segments and explain it logically.            

1.

#include

using namespace std;

 

class Base

{

public:

    virtual void show() { cout<<" In Base "; }

};

 

class Derived: public Base

{

public:

    void show() { cout<<"In Derived "; }

};

 

int main(void)

{

    Base *bp = new Derived;

    bp->show();

 

    Base &br = *bp;

    br.show();

 

    return 0;

}

2.

#include

using namespace std;

 

class Base

{

public:

    virtual void show() { cout<<" In Base "; }

};

 

class Derived: public Base

{

public:

    void show() { cout<<"In Derived "; }

};

 

int main(void)

{

    Base *bp, b;

    Derived d;

    bp = &d;

    bp->show();

    bp = &b;

    bp->show();

    return 0;

}


Step by Step Solution

3.46 Rating (149 Votes )

There are 3 Steps involved in it

1 Expert Approved Answer
Step: 1 Unlock

1 include usin... View full answer

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 Programming Questions!