Function Overriding in C++
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 falls under the category of Runtime Polymorphism.
Real-Life Example of Function Overriding
The 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
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 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;} |
Derived Function
Variations in Function Overriding
1. Call Overridden Function From Derived Class
C++
// 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 overridden function Parent::GeeksforGeeks_Print();CPP }}; int main(){ Child Child_Derived; Child_Derived.GeeksforGeeks_Print(); return 0;} |
Derived Function Base Function

The output of Call Overridden Function From Derived Class
2. 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;} |
Base Function
3. Access of Overridden Function to the Base Class
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_Derived2; Child_Derived.GeeksforGeeks(); // access GeeksforGeeks() function of the Base class Child_Derived.Parent::GeeksforGeeks(); return 0;} |
Derived Function Base Function

Access of Overridden Function to the Base Class
4. Access to Overridden Function
C++
// C++ Program Demonstrating// Accessing of Overridden Function#include <iostream>using namespace std; // defining of the Parent classclass Parent { public: // defining the overridden function void GeeksforGeeks_Print() { cout << "I am the Parent class function" << endl; }}; // defining of the derived classclass Child : public Parent {public: // defining of the overriding function void GeeksforGeeks_Print() { cout << "I am the Child class function" << endl; }}; int main(){ // create instances of the derived class Child GFG1, GFG2; // call the overriding function GFG1.GeeksforGeeks_Print(); // call the overridden function of the Base class GFG2.Parent::GeeksforGeeks_Print(); return 0;} |
I am the Child class function I am the Parent class function
Function Overloading Vs Function Overriding
Function Overloading | Function Overriding |
|---|---|
| It falls under Compile-Time polymorphism | It falls under Runtime Polymorphism |
| A function can be overloaded multiple times as it is resolved at Compile time | A function cannot be overloaded 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 to Function Overloading VS Function Overriding.






Please Login to comment...