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 name, return 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> 
using namespace std;

class Animal {
   public:
   virtual void speak() {
      cout << "Animal speaking" << endl;
   }
};

class Dog : public Animal {
   public:
   void speak() override // overriding speak function
   { 
      cout << "Dog barking" << endl; 
   }
};

int main(){
   Animal *animal = new Animal();
   Animal *dog = new Dog();

   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.

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>
using namespace std;

class Animal1{
   public:
   virtual void speak(){
      cout << "Animal Speaking" << endl;
   }
};

class Dog1 : public Animal1{
   public:
   void speek()    // Wrong function name
   { 
      cout << "Dog Barking" << endl;
   }
};

int main(){
   Animal1 *dog = new Dog1();
   dog->speak();

   delete dog;
   cout << endl;
}

The output of the above code is as follows −

Animal Speaking

In this example, the override specifier is used. Due to a mismatch in function name, it will give an error −

#include <iostream>
using namespace std;

class Animal1{
   public:
   virtual void speak(){
      cout << "Animal Speaking" << endl;
   }
};

class Dog1 : public Animal1 {
   public:
   void speek() override   // Wrong function name
   {
      cout << "Dog Barking" << endl;
   }
};

int main(){
   Animal1 *dog = new Dog1();
   dog->speak();

   delete dog;
   cout << endl;
}

The output of the above code is as follows −

main.cpp:16:10: error: void Dog1::speek() marked override, but does not override
16 |     void speek() override   // Wrong function name
  |          ^~~~~

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.

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>
using namespace std;

class Class1 {
   public:
   virtual void calculate(int x){
      cout << "Result: " << x * 2 << endl;
   }
};

class Class2 : public Class1 {
   public:
   void calculate(double x)    // Different parameter type
   { 
      cout << "Result: " << x * 3 << endl;
   }
};

int main(){
   Class1 *calc = new Class2();
   calc->calculate(5); 

   delete calc;
   cout << endl;
}

The output of the above code is as follows −

Result: 10

In this example, we have used override specifier. It will show an error as parameter type is different in both the functions −

#include <iostream>
using namespace std;

class Class1 {
   public:
   virtual void calculate(int x){
      cout << "Result: " << x * 2 << endl;
   }
};

class Class2 : public Class1 {
   public:
   void calculate(double x) override  // Different parameter type
   { 
      cout << "Result: " << x * 3 << endl;
   }
};

int main(){
   Class1 *calc = new Class2();
   calc->calculate(5); 

   delete calc;
   cout << endl;
}

The output of the above code is as follows −

main.cpp:16:10: error: 'void Class2::calculate(double)' marked 'override', but does not override
   16 |     void calculate(double x) override  // Different parameter type
      |          ^~~~~~~~~

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.

Advertisements