Function Overriding in C++
Last Updated :
30 Mar, 2023
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++
#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;
}
|
Variations in Function Overriding
1. Call Overridden Function From Derived Class
C++
#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;
Parent::GeeksforGeeks_Print();
}
};
int main()
{
Child Child_Derived;
Child_Derived.GeeksforGeeks_Print();
return 0;
}
|
Output
Derived Function
Base Function
The output of Call Overridden Function From Derived Class
2. Call Overridden Function Using Pointer
C++
#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;
Parent* ptr = &Child_Derived;
ptr->GeeksforGeeks();
return 0;
}
|
3. Access of Overridden Function to the Base Class
C++
#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();
Child_Derived.Parent::GeeksforGeeks();
return 0;
}
|
Output
Derived Function
Base Function
Access of Overridden Function to the Base Class
4. Access to Overridden Function
C++
#include <iostream>
using namespace std;
class Parent
{
public :
void GeeksforGeeks_Print()
{
cout << "I am the Parent class function" << endl;
}
};
class Child : public Parent
{
public :
void GeeksforGeeks_Print()
{
cout << "I am the Child class function" << endl;
}
};
int main()
{
Child GFG1, GFG2;
GFG1.GeeksforGeeks_Print();
GFG2.Parent::GeeksforGeeks_Print();
return 0;
}
|
Output
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 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 to Function Overloading VS Function Overriding.
Share your thoughts in the comments
Please Login to comment...