Function Overriding in C++

Function Overriding in C++ with Examples:

In this article, I am going to discuss Function Overriding in C++ with Examples. Please read our previous article where we discussed Polymorphism in C++ with Examples.

What is Function Overriding in C++?

The process of re-defining the superclass non-private method in the subclass with the same signature is called Function Overriding in C++. The same signature means the name and the parameters should be the same. The implementation of the subclass overrides (i.e. replaces) the implementation of the superclass method. The point that you need to keep in mind is that the overriding method is always going to be executed from the current class object. If this is not clear at the moment then don’t worry, we will try to explain this with some real-time examples.

Example to Understand Function Overriding in C++:

Let us understand Function Overriding in C++ with an Example. Please have a look at the below code. Here we have a class called Parent. It is having a function Display. This function will print “Display of Parent” when called. So, it will print a simple message on the screen. We don’t have anything more in this parent class.

Example to Understand Function Overriding in C++

Then we are creating a class called Child inheriting from the Parent class as shown in the below image. This Child class is inheriting publically from the Parent class.

Example to Understand Function Overriding in C++

See we have a Parent class which is having a function called display and we have a child class that is inherited from the parent class. That means the Child class will also get the display function.

Suppose we have created an object of the Parent class that is p and called the function display as p.display(), so it will display “Display of Parent”. Then we have created an object of the child class that is c and called the function c.display() then what it will display? See child class is not having any function display but it is borrowing from the parent class. So, the display function of the parent class will be called and print “Display of Parent”. For a better understanding, please have a look at the below image.

Example to Understand Function Overriding in C++

What is function Overriding in C++?

In our example, the child class is inheriting from the parent class so it is borrowing the display function. But if we override the display function once again in the child class, as follows

What is function Overriding in C++?

The Child class is already borrowing the function display from the parent class but it is not using that one. It is redefining its own version of the display function. So, the function is there but it is redefined. This is called Function Overriding.

So, what happens now let us see. If we create an object of the parent class and call the display function then the parent class display function will be called and if we create an object of the child class and call the display function then the child class display will be called, not the parent class function. For a better understanding, please have a look at the below image.

What is function Overriding in C++?

So, redefining a function of the parent class again in the child class is known as function overriding.

Real-Time Example to understand Function Overriding:

Now let us understand this conceptually. Let us take an example. Suppose we have two car design companies that are X and Y. So, the features available in X will also be available in Y. But Y company has redefined those features. So, writing functions once again is called function overriding.

Suppose windowpanes, sliding window panes, or closing window panes are already in ‘X’s design but it is not automatic, it is manual. But in ‘Y’s design, they have given automatic windowpane closing.

Suppose in ‘X’s car there is a key that you have to use to lock the car or open the car but in ‘Y’s car has the same feature that is you can open the door of the car with a keyless entry. So, you don’t have to unlock it using the key. So, the same feature is available that is opening and closing of doors of the car but it is redefined in the ‘Y’ car.

In the above example, our child class has its own version of the display function. This is the requirement of inheritance. So that is how we can bring a new feature to the derived classes.

Key points of Function Overriding in C++:
  1. Redefining a function of a base class in the derived class is called function overriding in C++.
  2. Function overriding is used for achieving runtime polymorphism.
  3. The prototype of an overrides function must be exactly the same as the base class function.
Example to Understand Function Overriding in C++
#include <iostream>
using namespace std;

class Parent
{
    public:
    void Display()
    {
        cout << "Display of Parent " << endl;
    }
};

class Child:public Parent
{
    public:
    void Display()
    {
        cout << "Display of Child " << endl;
    }
};

int main()
{
    Parent p;
    p.Display();
    Child c;
    c.Display();
    return 0;
}
Output:

Function Overriding in C++ with Examples

When do we need to override a function in C++?

If the superclass function logic is not fulfilling the sub-class business requirements, then the subclass needs to override that function with the required business logic. Usually, in most real-time applications, the superclass functions are implemented with generic logic which is common for all the next-level sub-classes.

When is a sub-class function treated as an overriding function in C++?

If a function in the sub-class contains the same signature as the superclass non-private function, then the subclass function is treated as the overriding function and the superclass function is treated as the overridden function.

In the next article, I am going to discuss Virtual Functions in C++ with Examples. Here, in this article, I try to explain Function Overriding in C++ with Examples and I hope you enjoy this Function Overriding in C++ with Examples article. I would like to have your feedback. Please post your feedback, question, or comments about this article.

Leave a Reply

Your email address will not be published. Required fields are marked *