Open In App

Advantages and Disadvantages of Function Overloading in C++

Function overloading is one of the important features of object-oriented programming. It allows users to have more than one function having the same name but different properties. Overloaded functions enable users to supply different semantics for a function, depending on the signature of functions.

Advantages of function overloading are as follows:



Disadvantage of function overloading are as follows:

Illustration:



int fun();
float fun();

 It gives an error because the function cannot be overloaded by return type only.

Example:




// Importing input output stream files
#include <iostream>
  
using namespace std;
  
// Methods to print
  
// Method 1
void print(int i)
{
  
    // Print and display statement whenever
    // method 1 is called
    cout << " Here is int " << i << endl;
}
  
// Method 2
void print(double f)
{
  
    // Print and display statement whenever
    // method 2 is called
    cout << " Here is float " << f << endl;
}
  
// Method 3
void print(char const* c)
{
  
    // Print and display statement whenever
    // method 3 is called
    cout << " Here is char* " << c << endl;
}
  
// Method 4
// Main driver method
int main()
{
  
    // Calling method 1
    print(10);
    // Calling method 2
    print(10.10);
    // Calling method 3
    print("ten");
  
    return 0;
}

Output
 Here is int 10
 Here is float 10.1
 Here is char* ten

Output Explanation:

In the above example all functions that were named the 3 same but printing different to which we can perceive there were different calls been made. This can be possible because of the arguments been passed according to which the function call is executed no matter be the functions are sharing a common name. Also, remember we can overload the function by changing their signature in return type. For example, here boolean print() function can be called for all three of them.


Article Tags :
C++