
- C++ Home
- C++ Overview
- C++ Environment Setup
- C++ Basic Syntax
- C++ Comments
- C++ Hello World
- C++ Omitting Namespace
- C++ Tokens
- C++ Constants/Literals
- C++ Keywords
- C++ Identifiers
- C++ Data Types
- C++ Numeric Data Types
- C++ Character Data Type
- C++ Boolean Data Type
- C++ Variable Types
- C++ Variable Scope
- C++ Multiple Variables
- C++ Input Output Operations
- C++ Basic Input/Output
- C++ Cin
- C++ Cout
- C++ Manipulators
- Type System & Data Representation
- C++ Modifier Types
- C++ Storage Classes
- C++ Constexpr Specifier
- C++ Numbers
- C++ Enumeration
- C++ Enum Class
- C++ References
- C++ Date & Time
- C++ Operators
- C++ Operators
- C++ Arithmetic Operators
- C++ Relational Operators
- C++ Logical Operators
- C++ Bitwise Operators
- C++ Assignment Operators
- C++ sizeof Operator
- C++ Conditional Operator
- C++ Comma Operator
- C++ Member Operators
- C++ Casting Operators
- C++ Pointer Operators
- C++ Operators Precedence
- C++ Unary Operators
- C++ Scope Resolution Operator
- C++ Control Statements
- C++ Decision Making
- C++ if Statement
- C++ if else Statement
- C++ Nested if Statements
- C++ switch Statement
- C++ Nested switch Statements
- C++ Loop Types
- C++ while Loop
- C++ for Loop
- C++ do while Loop
- C++ Foreach Loop
- C++ Nested Loops
- C++ Jump Statements
- C++ break Statement
- C++ continue Statement
- C++ goto Statement
- C++ Return Values
- C++ Strings
- C++ Strings
- C++ Loop Through a String
- C++ String Length
- C++ String Concatenation
- C++ String Comparison
- C++ Functions
- C++ Functions
- C++ Multiple Function Parameters
- C++ Recursive Function
- C++ Function Overloading
- C++ Function Overriding
- C++ Default Arguments
- C++ Arrays
- C++ Arrays
- C++ Multidimensional Arrays
- C++ Pointer to an Array
- C++ Passing Arrays to Functions
- C++ Return Array from Functions
- C++ Array Decay
- C++ Structure & Union
- C++ Structures
- C++ Unions
- C++ Class and Objects
- C++ Object Oriented
- C++ Classes & Objects
- C++ Class Member Functions
- C++ Class Access Modifiers
- C++ Static Class Members
- C++ Static Data Members
- C++ Static Member Function
- C++ Inline Functions
- C++ this Pointer
- C++ Friend Functions
- C++ Pointer to Classes
- C++ Constructors
- C++ Constructor & Destructor
- C++ Default Constructors
- C++ Parameterized Constructors
- C++ Copy Constructor
- C++ Constructor Overloading
- C++ Constructor with Default Arguments
- C++ Delegating Constructors
- C++ Constructor Initialization List
- C++ Dynamic Initialization Using Constructors
- C++ Destructors
- C++ Virtual Destructor
- C++ Inheritance
- C++ Inheritance
- C++ Multiple Inheritance
- C++ Multilevel Inheritance
- C++ Object-oriented
- C++ Overloading
- C++ Polymorphism
- C++ Abstraction
- C++ Encapsulation
- C++ Interfaces
- C++ Virtual Function
- C++ Pure Virtual Functions & Abstract Classes
- C++ Override Specifiers
- C++ Final Specifiers
- C++ Design Patterns
- C++ Creational Design Patterns
- C++ Singleton Design Pattern
- C++ Factory Method Design Pattern
- C++ Abstract Factory Pattern
- C++ Prototype Design Pattern
- C++ Structural Design Patterns
- C++ File Handling
- C++ Files and Streams
- C++ Reading From File
- C++ Advanced
- C++ Exception Handling
- C++ Dynamic Memory
- C++ Namespaces
- C++ Templates
- C++ Preprocessor
- C++ Signal Handling
- C++ Multithreading
- C++ Web Programming
- C++ Socket Programming
- C++ Concurrency
- C++ Advanced Concepts
- C++ Lambda Expression
- C++ nullptr
- C++ unordered_multiset
- C++ Structural Design Patterns
- C++ Adapter Pattern
- C++ Bridge Pattern
- C++ Composite Pattern
- C++ Decorator Pattern
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.