C++ | Virtual Functions | Question 3

Output of following program

filter_none

edit
close

play_arrow

link
brightness_4
code

#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, b;
    Derived d;
    bp = &d;
    bp->show();
    bp = &b;
    bp->show();
    return 0;
}

chevron_right


(A)

In Base 
In Base 

(B)

In Base 
In Derived

(C)

In Derived
In Derived

(D)

In Derived
In Base 


Answer: (D)

Explanation: Initially base pointer points to a derived class object. Later it points to base class object,

Quiz of this Question

Attention reader! Don’t stop learning now. Get hold of all the important DSA concepts with the DSA Self Paced Course at a student-friendly price and become industry ready.

My Personal Notes arrow_drop_up