Virtual functions in derived classes

In C++, once a member function is declared as a virtual function in a base class, it becomes virtual in every class derived from that base class. In other words, it is not necessary to use the keyword virtual in the derived class while declaring redefined versions of the virtual base class function.

For example, the following program prints “C::fun() called” as B::fun() becomes virtual automatically.





using namespace std;
class A {
    virtual void fun()
    { cout<<"\n A::fun() called ";}
class B: public A {
    void fun() 
    { cout<<"\n B::fun() called "; }      
class C: public B {
    void fun()
    { cout<<"\n C::fun() called "; }      
int main()
   C c; // An object of class C
   B *b = &c; // A pointer of type B* pointing to c
   b->fun();  // this line prints "C::fun() called"
   return 0;


Please write comments if you find anything incorrect, or you want to share more information about the topic discussed above

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
Article Tags :
Practice Tags :


Please write to us at to report any issue with the above content.