C++

In C++, There Is A Distinction Between Function Overloading And Function Overriding.

Although both function overloading and function overriding are forms of polymorphism, they are very distinct from one another. Let’s first talk a little bit about them before we explore the differences between them.

Overloading of Functions

We can have the same function used more than once in a program thanks to a feature called function overloading. Although they have the same name, overloaded functions must have a unique signature.

Example:

The identical function sum is declared four times in this example with various signatures. Which method is called when we call the function sum depends on the arguments we supply. It is also known as compile time polymorphism because this occurs during compilation. If you’re wondering why, during the function call in the example below, I prefixed each floating point value with the letter “f,” please see function overloading float problem.

#include <iostream>
using namespace std;
// overloaded functions
float sum(int, int);
float sum(float, float);
float sum(int, float);
float sum(float, int);
int main(){
   //This will call the second function
   cout<<sum(15.7f, 12.7f)<<endl;

   //This will call the first function
   cout<<sum(200, 100)<<endl;

   //This will call the third function
   cout<<sum(100, 20.7f)<<endl;

   //This will call the fourth function
   cout<<sum(90.8f, 30)<<endl;

   return 0;
}
float sum(int a, int b){
   return a+b;
}
float sum(float a, float b){
   return a+b;
}
float sum(int a, float b){
   return a+b;
}
float sum(float a, int b){
   return a+b;
}

Output:

28.4
300
120.7
120.8

Overriding a Function

A component of OOPs programming called function overriding enables us to override a parent class function in a child class.

Example:

#include<iostream>
using namespace std;
//Parent class or super class or base class
class A{
public:
   void disp() {
       cout<<"Parent Class disp() function"<<endl;
   }
   void xyz() {  
       cout<<"xyz() function of parent class";
   }
};
//child class or sub class or derived class
class B : public A{
public:
    /* Overriding disp() function of parent class
     * and giving a different definition to it.
     */
    void disp() {  
       cout<<"Child class disp() function"<<endl;
    }
};
int main(){
    //Creating object of child class B
    B obj;
    obj.disp();
    /* If you want to call the overridden function
     * (the same function which is present in parent class)
     * from the child class then assign the reference of
     * parent class to the child class object.
     */ 
    A obj2 = B();
    obj2.disp();
}

Output:

Child class disp() function
Parent Class disp() function

Function overloading and function overriding differ from one another.

After defining function overloading and overriding in C++ programming, let’s examine their differences:

1) When we declare identical functions with different arguments in the same class, function overloading occurs. When a child class overrides a parent class function, this is known as function overriding.

2) When using function overloading, each overloaded function’s signature should be unique. The signatures of the two functions (the overriding function and the overridden function) should match when overriding a function.

3) Since overloading occurs at compile time, it is often referred to as compile time polymorphism, whereas overriding occurs at run time and is hence referred to as run time polymorphism.

4) With function overloading, the number of overloaded functions is unlimited. In function overriding, the child class can only include one overriding function.

Related Articles

Leave a Reply

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

Back to top button