Override keyword in C++

In this tutorial, we will learn about the usage of the override keyword in C++. We are also going to see some code snippets for better understanding of this particular keyword.

What is function overriding?

Overriding is used in the case of inheritance of classes. When we redefine a base class function in its derived class with the same signatures, the function is said to be overridden.

In simpler words, both the base class and the derived class have a function with the same parameter list and the same return type.

The code below effectively demonstrates function overriding:

#include<iostream>
using namespace std;
class car
{
public:
    void wheels()
    {
        cout<<"car has four wheels\n";
    }
};

class bike:public car
{
public:
    void wheels()
    {
        cout<<"bike has two wheels"<<endl;
    }
};
int main()
{
    bike b;
    b.wheels();
    return 0;
}

The above code gives the output

bike has two wheels

But in some cases, the user may make a mistake while overriding the function. The function in the derived class may possess the same name as its parent class but may have a different parameter list or return types. To overcome these problems, C++11 has come up with the keyword override.

Override keyword

The override keyword serves two purposes:

  1. It shows the reader of the code that a virtual function of child class overrides a virtual function of its parent class.
  2. Specifying the keyword keeps the compiler aware that it is an override, and hence it checks for any method that may have the same name but not the same signature.

Why use the override keyword in C++?

To understand why the usage of the above keyword is important, let us first understand what happens on not using the keyword override.

Look at the sample code given below:

#include<iostream>
using namespace std;
class circle
{
public:
    float area()
    {
        int radius;
        cout<<"enter the radius: \n";
        cin>>radius;
        return 3.14*radius*radius;
    }
};

class square:public circle
{
public:
    int area()
    {
        int side;
        cout<<"enter the side of square: \n";
        cin>>side;
        return side*side;
    }
};

int main()
{
    circle c;
    square s;
    cout<<"compiled successfully\n";
    return 0;
}

This code gives the output

compiled successfully

Explanation: In the above code, the user intended to override the function area(). However, a small mistake was committed by the user as the return types of the functions in parent and derived class are not the same. This error was not detected by the compiler and the code is assumed to be executed successfully. By using the override keyword, such mistakes can be brought to the notice of the compiler.

Let us rewrite the above code and add the override keyword this time:

#include<iostream>
using namespace std;
class circle
{
public:
    float area()
    {
        int radius;
        cout<<"enter the radius: \n";
        cin>>radius;
        return 3.14*radius*radius;
    }
};

class square:public circle
{
public:
    int area() override
    {
        int side;
        cout<<"enter the side of square: \n";
        cin>>side;
        return side*side;
    }
};

int main()
{
    circle c;
    square s;
    cout<<"compiled successfully\n";
    return 0;
}

This time the compiler will detect the error and display the following message:

||=== Build file: "no target" in "no project" (compiler: unknown) ===|
C:\Users\alankrita\Documents\mistake.cpp|18|warning: override controls (override/final) only available with -std=c++11 or -std=gnu++11|
C:\Users\alankrita\Documents\mistake.cpp|18|error: 'int square::area()' marked 'override', but does not override|
||=== Build failed: 1 error(s), 1 warning(s) (0 minute(s), 0 second(s)) ===|

With this, we come to the end of this tutorial.

Also, see:

Leave a Reply

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