Override keyword in C++11

In this article, we are going to learn about one of the new features Override keyword in C++11. This keyword is introduced to help to specify the functions as an override in cases of Virtual functions. As we already have the support of virtual functions in C++ before C++11, so this new feature is just an enhancement to the concept of the virtual functions.

What are virtual functions?

It is important to learn “What are virtual functions?” in C++ and how they are implemented before you proceed with this article. Because understanding the Virtual functions is important to understand the ‘override’ keyword.

So let us understand this with some examples and in detail. From the concept of virtual functions, we know that we can declare a function as virtual in the base class, and then in a derived class, we can override the definition of the base class function. Before the introduction, of the override keyword, this was done just by declaring the base class virtual function in the derived class and providing its implementation as per the need of the derived class. Now with the introduction of the C++11 “override” keyword, we can specify the override in the function declaration in the derived class function.

C++ Program of virtual functions?

We are taking the below example of class inheritance to understand the override keyword. In this example, we have a class sampleClassA, and also we have two functions in this class. Function sum() is declared as virtual and the Multiply function is just a normal member function of this class.

class sampleClassA {
virtual void sum();
int Muliply();

Explanation of override keyword in C++

To expand our example, We have another class sampleClassB which is inheriting the sampleClassA class and its functions.If you look at the derived class, you will see we are overriding the sum() function. So to tell the compiler that this is an overridden function we are specifying the “override” keyword with the declaration of this function.

Similarly, we have another derived function Multiply. But as you can see when we try to use the override keyword with this function we get the compile from the compiler. The reason for this compile is, Multiply() function is not a virtual function in the Base class, so we can not use the override keyword with this function.

The third function in the derived class is Divide(), and with this function also we can not use the override keyword. The reason for this is very simple because this function is not a derived function which means this does not even qualify for a virtual override.

C++ Program Example

class sampleClassB : sampleClassA
void sum() override; // correct -- sampleClassB::sum overrides sampleClassA::sum
void Muliply() override; // error -- sampleClassA::multiply is not virtual function
void Divide() override; // error -- sampleClassB::Divide is not an inherited function

So I hope you could understand how we can use the override keyword in our code. So if we summarise the use and definitions then we can say that:


‘Override’ keyword Specifies that a virtual function overrides another virtual function. If the virtual function does not override a parent’s virtual function, then it throws a compiler error. So you can use the override keyword in the derived class for a function if that function is present in the base class and labeled as virtual. For functions that are not labeled as virtual, you can not use the override keyword.

Happy Learning!!