Function overriding is a feature that allows us to have a same function in child class which is already present in the parent class. A child class inherits the data members and member functions of parent class, but when you want to override a functionality in the child class then you can use function overriding. It is like creating a new version of an old function, in the child class.
Function Overriding Example
To override a function you must have the same signature in child class. By signature I mean the data type and sequence of parameters. Here we don’t have any parameter in the parent function so we didn’t use any parameter in the child function.
#include <iostream> using namespace std; class BaseClass { public: void disp(){ cout<<"Function of Parent Class"; } }; class DerivedClass: public BaseClass{ public: void disp() { cout<<"Function of Child Class"; } }; int main() { DerivedClass obj = DerivedClass(); obj.disp(); return 0; }
Output:
Function of Child Class
Note: In function overriding, the function in parent class is called the overridden function and function in child class is called overriding function.
How to call overridden function from the child class
As we have seen above that when we make the call to function (involved in overriding), the child class function (overriding function) gets called. What if you want to call the overridden function by using the object of child class. You can do that by creating the child class object in such a way that the reference of parent class points to it. Lets take an example to understand it.
#include <iostream> using namespace std; class BaseClass { public: void disp(){ cout<<"Function of Parent Class"; } }; class DerivedClass: public BaseClass{ public: void disp() { cout<<"Function of Child Class"; } }; int main() { /* Reference of base class pointing to * the object of child class. */ BaseClass obj = DerivedClass(); obj.disp(); return 0; }
Output:
Function of Parent Class
If you want to call the Overridden function from overriding function then you can do it like this:
parent_class_name::function_name
To do this in the above example, we can write following statement in the disp() function of child class:
BaseClass::disp();