Tutorialspoint.dev

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.

Source: http://www.umsl.edu/~subramaniana/virtual2.html

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

#include<iostream>
  
using namespace std;
  
class A {
  public:
    virtual void fun()
    { cout<<" A::fun() called ";}
};
  
class B: public A {
  public
    void fun() 
    { cout<<" B::fun() called "; }      
};
  
class C: public B {
  public:
    void fun()
    { cout<<" 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"
   getchar();
   return 0;
}

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



This article is attributed to GeeksforGeeks.org

tags:

C++ CPP

leave a comment

code

0 Comments

load comments

Subscribe to Our Newsletter