Updated March 27, 2023
Introduction to Function Overriding in C++
Function override is the concept of object-oriented programming language, suppose we have one method in a parent class and we are overriding that method in the child class with the same signature i.e. same method name, the same number of parameter and return type. We have provided our own implementation to this method specific to the child class, so we can say that the method can implement in two different classes.
Syntax
public class Parent{
access_modifier:
return_type method_name(){}
};
}
public class child : public Parent {
access_modifier:
return_type method_name(){}
};
}
In the above syntax we have one parent class and one child class, here child class is derived from the parent class which means the child can use all the functionality and properties which are present in the parent class. Let’s see one example for more understanding,
Example:
public class AnimalDemo{
public :
void foodForAnimal(){
cout<<"this method is going to have different implementation according to the food animal eat. So by override this method into the child class we can provide implementation accordingly.";
};
}
public class Cat: public AnimalDemo{
public:
void foodForAnimal(){
cout<<"Cat drink milk. This method is specific to this class.";
}
}
So in the above example, there can be various types of animals we have with different types of food they prefer so we can override this method can provide implementation according to animal food. But the method signature has to be the same. We can also pass parameter in it, these parameters also need to be exact in number like parent class.
How Function Overriding works in C++?
If we want to implement function overriding into our program first we need to have some kind of relation between two classes because we cannot implement function overriding within the same class, if we implement then it would be function overloading. So for function overriding we should have inheritance between two class i.e. parent and child relation.
But the question arises why we use function overriding?
The main reason behind this we can provide specific implementation to our method which is specific to that class. Function overriding can be achieved at runtime because which method will get call will be determined at runtime only. We can use parent class reference to call methods also. There is some case which shows which class method will get invoke according to the object created:
- Parent d = Child();:: In this case, the child class method will get a call because the parent class reference holding instance of the child class. But if the method does not present in the child class then the parent class method will get executed.
- Child c = Child();:: In this case also child class methods will get preference because it is holding the
- Child c = Parent();:: This is wrong we can do this compile-time error will get occur.
We have bind method call to its body then it is called binding. We have static and dynamic bindings.
Rule:
- There has to be a parent-child relationship between two classes.
- The method signature has to be matched. we cannot change the method name, method parameters or the return type everything has to be the same as the parent class signature.
- We can do the overriding method in child class only and ca provide a different implementation.
- In C++ we can also override the parent class member with the same signature they have in the class.
- We cannot override the static, private and final methods, because there scope limited to the class itself.
- We cannot even change the access of method while overriding from a parent class.
- Same in the case of return type also, we cannot change this too.
There is some advantage of method overriding which are mentioned below:
Suppose we have a parent class with so many children. But in the child class, they want to provide their own implementation of one single method so in that case function overriding is the choice. With function override, we can provide a different implementation of the same method without changing the code of parent class. For instance, we can take the example of the Bank with different rates of Interest they provide. The interest of banks varies from bank to bank.
So the bank is the parent class here and all the branches are a child for this with rateOfInterest() as the overriding method which provides the rate of interest detail to users.
Examples of Function Overriding in C++
Below are the examples of function overriding in C++:
Example #1
Code:
#include <iostream>
using namespace std;
class Parent {
public:
void m1(){
cout<<"This is the parent class method";
}
};
class Child: public Parent
{
public:
void m1()
{
cout<<"This is the child class method ";
}
};
int main(void) {
Child c = Child();
c.m1();
Parent p = Parent();
p.m1();
return 0;
}
Output:
Example #2
Code:
#include <iostream>
using namespace std;
class BankMain {
public:
int rate(int rate )
{
return rate;
}
};
class ICIC: public BankMain
{
public:
int rate(int rate )
{
return rate;
}
};
class HDFC: public BankMain
{
public:
int rate(int rate )
{
return rate;
}
};
class STND: public BankMain
{
public:
int rate(int rate )
{
return rate;
}
};
class AMEX: public BankMain
{
public:
int rate(int rate )
{
return rate;
}
};
int main(void) {
ICIC c = ICIC();
int icici = c.rate(10);
cout<<"Interest rate for icici bank is " << icici << "\n";
HDFC h = HDFC();
int hdfc = h.rate(20);
cout<<"Interest rate for hdfc bank is " << hdfc<< "\n";
STND s = STND();
int stnd = s.rate(50);
cout<<"Interest rate for stbd bank is " << stnd<< "\n";
AMEX a = AMEX();
int amex = a.rate(80);
cout<<"Interest rate for amex bank is " << amex<< "\n";
BankMain p = BankMain();
p.rate(4);
return 0;
}
Output:
Conclusion
So function overriding increases the code readability also make it easy to maintain. We do not need to modify the code of parent class or any other dependent child classes it is totally independent and easy to use.
Recommended Articles
This is a guide to Function Overriding in C++. Here we discuss the basic concept, syntax, examples and how Function Overriding works in C++? You can also go through our other suggested articles to learn more –