Function Overriding in C++

Do not miss this exclusive book on Binary Tree Problems. Get it now for free.

Reading time: 20 minutes | Coding time: 2 minutes

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 (compile time polymorphism). It is like creating a new version of an old function, in the child class.

Example

#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.

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() 
{
   BaseClass obj = DerivedClass(); 
   obj.disp();
   return 0;
}

Output :

Function of Parent Class

Example

#include <iostream>

using namespace std;

class Aoperations
{
	public:
		void fun()
		{   
			cout << "Main class of Arthimetic operations" << endl;
		}
};

class Addition : public Aoperations
{
	public:
		void fun()
		{
			cout << "You opted for addition of numbers" << endl;
		}
};

int main()
{
    Aoperations a1;
    a1.fun();
	Addition a2;
	a2.fun();
	return 0;
}

Output :

Main class of Arthimetic operations
You opted for addition of numbers

In the above example we have fun function which are declared in different classes where Aoperations is the parent class and is publically inherited by Addition which is the base class. As Addition class inherits the base class publically then we are left with 2 fun functions in Addition class and this results in function overriding. The respective function of the required class can be called with the help of the object as potrayed in the example.

How Addition class actually looks

class Addition : public Aoperations
{
	public:
     
		void fun()  //Aoperation class fun
		{   
			cout << "Main class of Arthimetic operations" << endl;
		}

		void fun()  //Addition class fun
		{
			cout << "You opted for addition of numbers" << endl;
		}
}

Difference between function overloading and function overriding

1)Inheritance: Overriding of functions occurs when one class is inherited from another class. Overloading can occur without inheritance.

2)Function Signature(orderand type of parameter):In overriding, function signatures must be same.While in overloaded functions must differ in function signature ie either number of parameters or type of parameters should differ.

3)Scope of functions: Overridden functions are in different scopes; whereas overloaded functions are in same scope.

4)Behavior of functions: Overriding is needed when derived class function has to do some added or different job than the base class function. Overloading is used to have same name functions which behave differently depending upon parameters passed to them.

Points to remember for Function Overriding in C++

  1. Inheritance should be there. Function overriding cannot be done within a class. For this we require a derived class and a base class.

  2. In C++, the base class member can be overridden by the derived class function with the same signature as the base class function.

3)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.

4)Method overriding is used to provide different implementations of a function so that a more specific behavior can be realized.

5)Overriding is done in a child class for a method that is written in the parent class.

6)It changes the existing functionality of the method hence should carefully be used.

Question

Which of the following cannot be overloaded in C++?

Destructor
Incrementor
Constructor
new and delete
Destructor of a class cannot be overloaded in C++ programming as it is the last function to be invoked on an OBJECT. It means the object is dead after calling the destructor,as the purpose of destructor is to do clean up. The objects of a class have similar behaviour.So eventually all adhere to same method of destruction. Hence, it is not required to override the destructor.

Sign up for FREE 3 months of Amazon Music. YOU MUST NOT MISS.