Function Overriding in C++

In this tutorial we will study and understand the concept and working of Function Overriding in C++. Function Overriding is a type of Polymorphism. Polymorphism in C++ is the ability of one function / operator to have multiple and different implementations. Function Overloading is one example of static polymorphism. We will understand Polymorhpism in more detail in other tutorials.

Function Overriding in C++

Function overriding, in object oriented programming, is a language feature that allows a subclass or child class to provide a specific implementation of a method that is already provided by one of its super classes or parent classes.
If we inherit a class into the derived class and provide a definition for one of the base class’s function again inside the derived class, then that function is said to be overridden, and this mechanism is called Function Overriding.

 

Function Overriding in C++

Points to remember for for Function Overriding in C++
  • Inheritance should be there. Function overriding cannot be done within a class. For this we require a derived class and a base class.
  • In C++, the base class member can be overridden by the derived class function with the same signature as the base class function.
  • Function that is redefined must have exactly the same declaration in both base and derived class, that means same name, same return type and same parameter list.
  • Method overriding is used to provide different implementations of a function so that a more specific behavior can be realized.
  • Overriding is done in a child class for a method that is written in the parent class.
  • It changes the existing functionality of the method..
Function Call Binding with class Objects

Connecting the function call to the function body is called Binding. When it is done before the program is run, its called Early Binding or Static Binding or Compile-time Binding.

Example Program of Function Overriding in C++

Run Online

#include<iostream>
using namespace std;
class Base
{
 	public:
 	void show()
 	{
  		cout << "Base class\t";
 	}
};
class Derived:public Base
{
 	public:
 	void show()
 	{
  		cout << "Derived Class";
 	}
};

int main()
{
   Base b;       //Base class object
   Derived d;     //Derived class object
   b.show();     //Early Binding Occurs
   d.show();   
}
Output
Base class    Derived Class
Program Explanation:

Here we created a class named Base class and another class named Derived class which inherits all properties of base class publicly. Both classes have same function named show with same function prototype, however both print different message according to their respective class’s. Thus the functionality is different. In the main function we created 2 objects of each of these classes and called their respective show functions. when the function of the derived class show is called, it overrides the base function show and prints “Derived class” instead of Base class. This is Function overriding.

Function Overriding using Base Class Pointer & Virtual Function

Run Online

#include<iostream>
using namespace std;
class Base
{
 public:
 virtual void show() // virtual function
 {
  cout << "Base class";
 }
};
class Derived:public Base
{
 public:
 void show()
 {
  cout << "Derived Class";
 }
};

int main()
{
 Base* b;       //Base class pointer
 Derived d;     //Derived class object
 b = &d;	// passing derived class address into base class pointer	
 b->show();     //Late Binding Occurs
}
Output
Total Objects: 1
Derived Class
Program Explanation:

In this program,we made the base class’s show function as virtual. This enables us to use the base class’s object to call the functions of derived class which are overridden and virtual. Thus in the main function you can see that we created a base class pointer object and made it to point derived class object by passing derived class objects address. then using the (->) operator we called the derived class objects show function which prints the output as “Derived Class”. This is known as Dynamic Polymorphism or Late binding.

Watch it on YouTube

Leave a Reply

Your email address will not be published. Required fields are marked *