override identifier in C++
Last Updated :
27 Nov, 2024
Function overriding is a redefinition of the base class function in its derived class with the same signature i.e. return type and parameters.
But there may be situations when a programmer makes a mistake while overriding that function. So, to keep track of such an error, C++11 has come up with the override identifier.
If the compiler comes across this identifier, it understands that this is an overridden version of the function of the base class. It will make the compiler check the base class to see if there is a virtual function with this exact signature. And if there is not, the compiler will show an error.
Let's understand through the following example:
CPP
// A CPP program without override keyword, here
// programmer makes a mistake and it is not caught
#include <iostream>
using namespace std;
class Base {
public:
// user wants to override this in
// the derived class
virtual void func() { cout << "I am in base" << endl; }
};
class derived : public Base {
public:
// did a silly mistake by putting
// an argument "int a"
void func(int a)
{
cout << "I am in derived class" << endl;
}
};
// Driver code
int main()
{
Base b;
derived d;
cout << "Compiled successfully" << endl;
return 0;
}
OutputCompiled successfully
Explanation: Here, the user intended to override the function func() in the derived class but did a silly mistake and redefined the function with a different signature. Which was not detected by the compiler. However, the program is not actually what the user wanted. So, to get rid of such silly mistakes to be on the safe side, the override identifier can be used.
Below is a C++ example to show the use of override identifier in C++.
CPP
// A CPP program that uses override keyword so
// that any difference in function signature is
// caught during compilation
#include <iostream>
using namespace std;
class Base
{
public:
// user wants to override this in
// the derived class
virtual void func()
{
cout << "I am in base" << endl;
}
};
class derived : public Base
{
public:
// did a silly mistake by putting
// an argument "int a"
void func(int a) override
{
cout << "I am in derived class" << endl;
}
};
int main()
{
Base b;
derived d;
cout << "Compiled successfully" << endl;
return 0;
}
Output(Error)
main.cpp:23:10: error: ‘void derived::func(int)’ marked ‘override’, but does not override
23 | void func(int a) override
| ^~~~
In short, it serves the following functions. It helps to check if:
- There is a method with the same name in the parent class.
- The method in the parent class is declared as "virtual" which means it was intended to be rewritten.
- The method in the parent class has the same signature as the method in the subclass.
Similar Reads
C# Identifiers In programming languages, identifiers are used for identification purposes. Or in other words, identifiers are the user-defined name of the program components. In C#, an identifier can be a class name, method name, variable name, or label. Example: public class GFG { static public void Main () { int
2 min read
dot (.) operator in C++ The C++ dot (.) operator is used for direct member selection via the name of variables of type class, struct, and union. It is also known as the direct member access operator. It is a binary operator that helps us to extract the value of the function associated with a particular object, structure, o
3 min read
How to Get a Unique Identifier For Object in C++? Prerequisite: Classes and Objects in C++ A single entity within a given system is identified by a string of numbers or letters called a unique identifier (UID). UIDs enable addressing of that entity, allowing access to and interaction with it. There are a few choices, depending on your "uniqueness"
3 min read
#define in C++ In C++, #define is a preprocessor directive used to define a macro. Macros are a way to represent a fragment of code or a constant value by giving it a name. When the preprocessor encounters the macro name in the code, it replaces it with the corresponding code fragment or value that is defined usin
4 min read
Can main() be overloaded in C++? Predict the output of following C++ program. C highllight=12-5 #include <iostream> using namespace std; int main(int a) { cout << a << "\n"; return 0; } int main(char *a) { cout << a << endl; return 0; } int main(int a, int b) { cout << a << "
2 min read
Const keyword in C++ In this article, the various functions of the const keyword which is found in C++ are discussed. Whenever const keyword is attached with any method(), variable, pointer variable, and with the object of a class it prevents that specific object/method()/variable to modify its data items value.Constant
10 min read