Function Overriding in C++
Function Overriding in C++
On this page we will discuss about function overriding in C++ . The function in child class overrides the inherited function in the parent class.This is known as function overriding.There are two methods to do function overriding generic and virtual function based overriding .What is Function Overriding?
- Using inheritance a child class can inherit features (data members/member functions) from a parent class. Imagine if, the same function is defined in both the parent class and the child class.
- Now if we call this function using the object of the child class, the function of the child class is executed.
- This is known as function overriding in C++. The function in child class overrides the inherited function in the Parent class
- There are two methods to do function overriding –
- Generic function overriding
- Virtual function based overriding
Advantages of function overriding in C++
As we have discussed above what is function overriding, now let’s look at some of its advantages –
- The child class having the same function as the parent class, can even have its independent implementation of that function.
- Helps in saving memory space.
- Maintain consistency, and readability of our code.
- Helps in making code re-usability easy.
- The child class can access the function of the parent class as well.
Disadvantages of function Overriding in C++
After learning things about function overriding lets have a look on its disadvantage:-
- Function overriding cannot be done within a same class. So, we need to do implement inheritance.
- Static methods can never be overridden.
class Base { static void fun() }; class Derived : public Base { void fun() // error };
Code to show Function Overriding (Method 1)
#include <iostream> using namespace std; class Parent { public: void display() { cout << "Parent Function" << endl; } }; class Child : public Parent { public: void display() { cout << "Child Function" << endl; } }; int main() { Child childObj; childObj.display(); return 0; }
Output
Child Function
Using Virtual Function to Achieve Function Overriding (Method 2)
For a function display() which is defined in both parent & child classes. If we create a pointer of Parent type and point it to an object of the child class. It will –
- Always call the parent class function
- Will never override parent class function
As demonstrated in the example given below –
#include <iostream> using namespace std; class Parent { public: void print () { cout<<"Parent Class"<<endl; } }; class Child:public Parent { public: void print () { cout<<"Child Class"<<endl; }}; int main () { Child c2; Parent &p2 = c2; p2.print (); return 0; }
Output
Parent Class
How to solve this?
We use a virtual function in the parent class in order to ensure that the parent function is overridden. It is done by adding virtual keyword before function declaration in the parent class.
Code
#include <iostream> using namespace std; class Parent { public: virtual void print () { cout << "Parent Class" << endl; } }; class Child:public Parent { public: void print () { cout << "Child Class" << endl; } }; int main () { Child c2; Parent &p2 = c2; p2.print (); return 0; }
Output
Child Class
Prime Course Trailer
Related Banners
Get PrepInsta Prime & get Access to all 200+ courses offered by PrepInsta in One Subscription
Get over 200+ course One Subscription
Courses like AI/ML, Cloud Computing, Ethical Hacking, C, C++, Java, Python, DSA (All Languages), Competitive Coding (All Languages), TCS, Infosys, Wipro, Amazon, DBMS, SQL and others
Login/Signup to comment