Summary: In this tutorial, we will learn what virtual function and function overriding are in C++ and how can we override a function in C++.

Introduction to Function Overriding

When we redefine a base class’s function in the derived class with the same function signature but with a different implementation, it is called function overriding.

Example:

class Base{
public:
    void display(){
        cout << "This is Base Class\n";
    }
};

class Derived: public Base{
public:

    void display(){
        cout << "This is Derieved Class\n";
    }
};

In this example, we have overriden the base class’s display method in the derived class.

Function overriding is an example of runtime polymorphism because the binding of the function call statement to the matching function happens during the runtime of the program

Note: We cannot override a derived class method in the base class because it is the opposite of the inheritance.

What is a Virtual Function?

In C++, only redefining a base class’s method in the derived class is not enough.

Consider the following example for the instance:

#include <iostream>
using namespace std;
 
class Base{
public:
    void display(){
        cout << "This is Base \n";
    }
 
};
 
class Derived: public Base{
public:
    //Overriding the base class's display function
    void display(){
        cout << "This is Derieved \n";
    }
};
 
int main()
{
    //Creating a Derived class object using Base class Reference
    Base *obj = new Derived(); 
    obj->display();
 
    return 0;
}

Output:

virtual function in C++

In this program, although we have redefined the display function in the derived class, the version of the base class of the display function is still being invoked.

This is because we have not declared the display function of the base class as a virtual function.

By defining a function as virtual, it is ensured that the derived class version will be used if the same version of the function exists in the derived class, regardless of the reference (or pointer) type used for the function call.

In short, we should declare a function as virtual in the base class if we intend to override it in the derived class.

Syntax for the Virtual Function:

virtual <return_type> <function_name>(<arguments>){
    //code
}

Example:

virtual void display(){
    cout << "This is Base \n";
}

C++ Example: Function Overriding and Virtual Function

If we rewrite the previous program by defining the display function of the base class as virtual, we get the appropriate output.

#include <iostream>
using namespace std;
 
class Base{
public:
    virtual void display(){
        cout << "This is Base \n";
    }
 
};
 
class Derived: public Base{
public:
    //Overriding the base class's display function
    void display(){
        cout << "This is Derieved \n";
    }
};

int main()
{
    //Creating a Derived class object using Base class Reference
   Derived obj; 
    obj.display();
 
    return 0;
}

Output:

virtual function example

This time we have successfully overridden the base class’s display() function using the virtual keyword.

Conclusion

Function overriding in C++ is a runtime polymorphism, where we redefine a base class’s method in the derived class.

When working with reference or pointer objects, we should declare a function as virtual in the base class, if we intend to override it in the derived class.

Leave a Reply