Last Updated : 20 May, 2025
A function is a block of statements that together performs a specific task by taking some input and producing a particular output. Function overriding in C++ is termed as the redefinition of base class function in its derived class with the same signature i.e. return type and parameters. It can be of both type: Compile Time and Runtime Polymorphism.
What is Function Overriding in C++?Function overriding is a type of polymorphism in which we redefine the member function of a class which it inherited from its base class. The function signature remains same but the working of the function is altered to meet the needs of the derived class. So, when we call the function using its name for the parent object, the parent class function is executed. But when we call the function using the child object, the child class version will be executed.
Real-Life Example of Function OverridingThe best Real-life example of this concept is the Constitution of India. India took the political code, structure, procedures, powers, and duties of government institutions and set out fundamental rights, directive principles, and the duties of citizens of other countries and implemented them on its own; making it the biggest constitution in the world.
Another Development real-life example could be the relationship between RBI(The Reserve Bank of India) and Other state banks like SBI, PNB, ICICI, etc. Where the RBI passes the same regulatory function and others follow it as it is.
Function Overriding Function Overriding using Virtual FunctionFunction overriding is performed at the runtime, which means that function call will be binded to its definition during runtime (also known as late binding or dynamic binding). This can be done with the help of virtual functions.
Syntax
class Base {
public:
virtual func()
{
// definition
}
};
class Derived : public Base {
public:
func() override
{
// new definition
}
};
Here, override keyword tells the compiler that the given overridden function should be declared as virtual in the parent class. It is a kind of double check as the program can compile without errors even if the function is not virtual. But then, it will be compile time polymorphism and we won't get the desired behaviour of the function overriding.
// C++ Program to illustrate how to implement
// function overriding using virtual function
#include <iostream>
using namespace std;
class Base {
public:
// Declare the function as virtual to allow overriding
// in derived classes
virtual void display()
{
cout << "Display method of Base class" << endl;
}
// Virtual destructor to ensure proper cleanup of
// derived class objects
virtual ~Base() {}
};
class Derived : public Base {
public:
// Override the display method
void display() override
{
cout << "Display method of Derived class" << endl;
}
};
int main()
{
Base* basePtr;
Derived derivedObj;
// Point base class pointer to derived class object
basePtr = &derivedObj;
// Call the display function
// This will call the display method of the Derived
// class due to the virtual function mechanism
basePtr->display();
return 0;
}
Display method of Derived classAdvantages of Function Overriding
The following are the main advantages of function overriding:
While runtime function overriding provides numerous advantages, it also comes with certain limitations:
// C++ program to demonstrate function overriding
// by calling the overridden function
// of a member function from the child class
#include <iostream>
using namespace std;
class Parent {
public:
void GeeksforGeeks_Print()
{
cout << "Base Function" << endl;
}
};
class Child : public Parent {
public:
void GeeksforGeeks_Print()
{
cout << "Derived Function" << endl;
// call of original function
Parent::GeeksforGeeks_Print();
}
};
int main()
{
Child Child_Derived;
Child_Derived.GeeksforGeeks_Print();
return 0;
}
Derived Function Base FunctionThe output of Call Overridden Function From Derived Class Example 2: C++ Program to Call Overridden Function Using Pointer C++
// C++ program to access overridden function using pointer
// of Base type that points to an object of Derived class
#include <iostream>
using namespace std;
class Parent {
public:
void GeeksforGeeks()
{
cout << "Base Function" << endl;
}
};
class Child : public Parent {
public:
void GeeksforGeeks()
{
cout << "Derived Function" << endl;
}
};
int main()
{
Child Child_Derived;
// pointer of Parent type that points to derived1
Parent* ptr = &Child_Derived;
// call function of Base class using ptr
ptr->GeeksforGeeks();
return 0;
}
Example 3: C++ Program to Access of Overridden Function using Child Class Object C++
// C++ program to access overridden function
// in main() using the scope resolution operator ::
#include <iostream>
using namespace std;
class Parent {
public:
void GeeksforGeeks()
{
cout << "Base Function" << endl;
}
};
class Child : public Parent {
public:
void GeeksforGeeks()
{
cout << "Derived Function" << endl;
}
};
int main()
{
Child Child_Derived;
Child_Derived.GeeksforGeeks();
// access GeeksforGeeks() function of the Base class
Child_Derived.Parent::GeeksforGeeks();
return 0;
}
Derived Function Base FunctionAccess of Overridden Function to the Base Class Overriding without Virtual Function
Virtual function is essential for function overriding. If we don't use virtual function, the function call and the definition is bound at the compilation of the program. This is not overriding but instead, the redefined function only hides the base class function with the same name in the derived class making it behave like an overridden function in some cases.
Syntax
class Parent {
access_modifier :
// overridden function
return_type name_of_the_function() {}
};
class child : public Parent {
access_modifier :
// overriding function
return_type name_of_the_function() {}
};
Example C++
// C++ program to demonstrate compile time function overriding
#include <iostream>
using namespace std;
class Parent {
public:
void GeeksforGeeks_Print()
{
cout << "Base Function" << endl;
}
};
class Child : public Parent {
public:
void GeeksforGeeks_Print()
{
cout << "Derived Function" << endl;
}
};
int main()
{
Child Child_Derived;
Child_Derived.GeeksforGeeks_Print();
return 0;
}
Refer to this article to know more - Hiding of all Overloaded Methods with Same Name in Base Class
Function Overloading Vs Function OverridingFunction Overloading
Function Overriding
It falls under Compile-Time polymorphism It can be both Compile Time or Runtime Polymorphism A function can be overloaded multiple times as it is resolved at Compile time A function cannot be overridden multiple times as it is resolved at Run time Can be executed without inheritance Cannot be executed without inheritance They are in the same scope They are of different scopes.To know more, you can refer toFunction Overloading VS Function Overriding.
RetroSearch is an open source project built by @garambo | Open a GitHub Issue
Search and Browse the WWW like it's 1997 | Search results from DuckDuckGo
HTML:
3.2
| Encoding:
UTF-8
| Version:
0.7.4