Multiple choice

What will be the output of following code? class Base { public: void display() {cout<<”t base d”; } virtual void show() { cout<<”t base s”; } }; Class Derived: public Base { public: void display() { cout<<”t derived d”; } void show() { cout<<”t derived s”; } }; int main() { Base B; Derived D; Base *bptr;bptr=&B; bptr->display(); bptr->show(); bptr=&D; bptr-> display(); bptr-> show(); return 0; }

  1. base d base s derived d derived s

  2. base d base s base d base s

  3. base d base s base d derived s

  4. none of the above

  5. all a, b and c

Reveal answer Fill a bubble to check yourself
C Correct answer
Explanation

The show() function is defined as virtual while display() function is not defined as virtual. When function is made virtual, C++ determines which function to use at run time based on the type of object pointed by the base class.