Override Specifier in C++

Function overriding is an example of run-time polymorphism in C++, where we redefine the function of a base class in the derived class. The function namereturn type, and parameters should be same in base as well as derived class functions.

What is override Specifier?

The override specifier in C++ is used to override a function and it makes sure that overriding is done correctly. It is useful in finding the mistakes while overriding a function.

If the override specifier is not used while overriding, then the function will run normally without giving any error as it will consider it a different function. So, we use an override specifier to overcome this problem, as the compiler will generate an error if the function is not overridden correctly.

Function Overriding using override Specifier

Here is an example of function overriding of speak() function using override specifier:

#include<iostream> usingnamespace std;classAnimal{public:virtualvoidspeak(){
      cout <<"Animal speaking"<< endl;}};classDog:public Animal{public:voidspeak()override// overriding speak function{ 
      cout <<"Dog barking"<< endl;}};intmain(){
   Animal *animal =newAnimal();
   Animal *dog =newDog();

   animal->speak(); 
   dog->speak();delete animal;delete dog;
   cout << endl;}

The output of the above code is as follows −

Animal speaking
Dog barking

Rules of Using override Specifier

You need to follow some rules while using the override specifier. These rules are mentioned below −

  • The function in the base class needs to be a virtual function.
  • The function name and signatures (number of parameters and their types, return type, and const qualifiers) should be same, otherwise it will give an error.

Benefits of Using override Specifier

The benefit of using an override specifier is that it ensures correct implementation of the function overriding. It can solve following problems if override specifier is used −

Using Wrong Function Name

When you make a mistake while writing a function name, the program will run normally if override specifier is not used. With override specifier, it will give an error.

Without override With override

In this example, there is a typo in the function name. Instead of showing any error, it will run the speak() function of Animal class −

#include <iostream>usingnamespace std;classAnimal1{public:virtualvoidspeak(){
      cout <<"Animal Speaking"<< endl;}};classDog1:public Animal1{public:voidspeek()// Wrong function name{ 
      cout <<"Dog Barking"<< endl;}};intmain(){
   Animal1 *dog =newDog1();
   dog->speak();delete dog;
   cout << endl;}

The output of the above code is as follows −

Animal Speaking

Wrong Parameter Type

If you define a wrong parameter type while overriding, the program will run normally if override specifier is not used. With override specifier, it will give an error.

Without override With override

In this example, we want to call the calculate() function of class2, but due to a different parameter type, calculate() function of Class1 is called without displaying any error −

#include <iostream>usingnamespace std;classClass1{public:virtualvoidcalculate(int x){
      cout <<"Result: "<< x *2<< endl;}};classClass2:public Class1{public:voidcalculate(double x)// Different parameter type{ 
      cout <<"Result: "<< x *3<< endl;}};intmain(){
   Class1 *calc =newClass2();
   calc->calculate(5);delete calc;
   cout << endl;}

The output of the above code is as follows −

Result: 10

Conclusion

The override specifier in C++ is used to avoid function overriding mistakes. It is always used with virtual functions and ensures that the function overriding is implemented correctly without any errors.

Comments

Leave a Reply

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