Function Overriding in C++ with examples
Function overriding is a feature that allows us to have the same function in child class which is already present in the parent class. A child class inherits the data members and member functions of the parent class, but when you want to override 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 the following statement in the disp() function of child class:
BaseClass::disp();
Related Posts:
- C++ Tutorial – Learn C++ Programming with examples
- Hello World – First C++ Program
- Variables in C++
- Data Types in C++
- Operators in C++
- Recursion in C++ with example
- Functions in C++ with example
- goto statement in C++ with example
- Break statement in C++ with example
- Continue Statement in C++ with example
- do-while loop in C++ with example
- While loop in C++ with example
- For loop in C++ with example
- Switch Case statement in C++ with example
- If else Statement in C++
- Constructors in C++
- Enumeration in C++
- OOPs Concepts in C++
- this pointer in C++
- Pointers in C++
- Strings in C++
- Passing Array to Function in C++
- Arrays in C++
- Difference between Function Overloading and Function overriding in C++
- Function overloading in C++ with examples
- Polymorphism in C++ and its types
- Inheritance in C++ with examples
- Destructors in C++ with examples
- Encapsulation in C++ with example
- Friend Class and Friend Functions in C++
- Interfaces in C++ with Examples
- Abstraction in C++ with example