Skip to content
Related Articles

Related Articles

Improve Article
Save Article
Like Article

C++ | Virtual Functions | Question 13

  • Difficulty Level : Hard
  • Last Updated : 28 Jun, 2021


Take a step-up from those "Hello World" programs. Learn to implement data structures like Heap, Stacks, Linked List and many more! Check out our Data Structures in C course to start learning today.



#include <iostream>
using namespace std;
   
class A
{
public:
    virtual void fun() { cout << "A::fun() "; }
};
   
class B: public A
{
public:
   void fun() { cout << "B::fun() "; }
};
   
class C: public B
{
public:
   void fun() { cout << "C::fun() "; }
};
   
int main()
{
    B *bp = new C;
    bp->fun();
    return 0;
}

(A) A::fun()
(B) B::fun()
(C) C::fun()


Answer: (C)

Explanation: The important thing to note here is B::fun() is virtual even if we have not uses virtual keyword with it.

When a class has a virtual function, functions with same signature in all descendant classes automatically become virtual. We don’t need to use virtual keyword in declaration of fun() in B and C. They are anyways virtual.

Quiz of this Question

My Personal Notes arrow_drop_up
Recommended Articles
Page :

Start Your Coding Journey Now!