Open In App

Advantages and Disadvantages of Function Overloading in C++

Improve
Improve
Like Article
Like
Save
Share
Report

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:

  • The main advantage of function overloading is that it improves code readability and allows code reusability.
  • The use of function overloading is to save memory space, consistency, and readability.
  • It speeds up the execution of the program
  • Code maintenance also becomes easy.
  • Function overloading brings flexibility to code.
  • The function can perform different operations and hence it eliminates the use of different function names for the same kind of operations.

Disadvantage of function overloading are as follows:

  • Function declarations that differ only in the return type cannot be overloaded

Illustration:

int fun();
float fun();

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

  • Member function declarations with the same name and the same parameter types cannot be overloaded if any of them is a static member function declaration.
  • The main disadvantage is that it requires the compiler to perform name mangling on the function name to include information about the argument types.

Example:

C++




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



Last Updated : 23 Mar, 2021
Like Article
Save Article
Previous
Next
Share your thoughts in the comments
Similar Reads