Write output of given code segments and explain it logically, answer without explanation will be awarded zero marks.
a)
#include<iostream>
using namespace std;
class Base
{
public:
virtual void show() { cout<<" In Base \n"; }
};
class Derived: public Base
{
public:
void show() { cout<<"In Derived \n"; }
};
int main(void)
{
Base *bp = new Derived;
bp->show();
Base &br = *bp;
br.show();
return 0;
}
Output
In Derived
In Derived
Explanation
The virtual function in the base class is overridden by the show function in the base class hence the "In Derived" will be printed in both cases
Comments
Leave a comment