Function overriding in C++

What is function overriding in C++?

Let us look at Function Overriding in C++. There are two methods to do function overriding – 

  1. Generic function overriding
  2. Virtual function based overriding
function overriding in C++

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

Function Overriding in C++

Code to show Function Overriding (Method 1)

// C++ program to demonstrate function overriding
// Child class object overrides the inherited implimentation
// of parent class display function and executes its own
#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 Scope Resolution to Specify function call

// using scope resolution operator to specify using parent class method 
#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();
    
    // using scope resolution operator to specify using parent class method 
    childObj.Parent::display();
    
    return 0;
}

Output

Child Function
Parent 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;

// Defining the parent class
class Parent 
{
    public: 
    void print() 
    { 
        cout << "Parent Class" << endl;
    } 
};

// Defining the child classs 
class Child : public Parent 
{
    public:
    // print redeclared in child class
    void print()
    {  
        cout << "Child Class" << endl; 
}
};

int main()
{
Child c2;
Parent &p2 = c2;

p2.print();

// can also do this, would be the same thing as above
// Parent *parent = new Child();
// parent->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

// Demonstrating function overriding with virtual function in C++
#include<iostream>
using namespace std;

// Defining the parent class
class Parent 
{
    public:
    // adding virtual keyword here
    virtual void print() 
    { 
        cout << "Parent Class" << endl;
    } 
};

// Defining the child classs 
class Child : public Parent 
{
    public:
    // print redeclared in child class
    void print()
    {  
        cout << "Child Class" << endl; 
}
};

int main()
{
Child c2;
Parent &p2 = c2;

p2.print();

// can also do this, would be the same thing as above
// Parent *parent = new Child();
// parent->print(); return 0; }

Output

Child Class

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
};