Open In App
Related Articles

C++ | Virtual Functions | Question 3

Improve Article
Improve
Save Article
Save
Like Article
Like

Output of following program




#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;
}


(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

Whether you're preparing for your first job interview or aiming to upskill in this ever-evolving tech landscape, GeeksforGeeks Courses are your key to success. We provide top-quality content at affordable prices, all geared towards accelerating your growth in a time-bound manner. Join the millions we've already empowered, and we're here to do the same for you. Don't miss out - check it out now!

Last Updated : 28 Jun, 2021
Like Article
Save Article
Previous
Next
Similar Reads