Advantages and Disadvantages of Function Overloading in C++ - GeeksforGeeks

23 Mar, 2021
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 1void print(int i){      // Print and display statement whenever    // method 1 is called    cout << " Here is int " << i << endl;}  // Method 2void print(double f){      // Print and display statement whenever    // method 2 is called    cout << " Here is float " << f << endl;}  // Method 3void print(char const* c){      // Print and display statement whenever    // method 3 is called    cout << " Here is char* " << c << endl;}  // Method 4// Main driver methodint main(){      // Calling method 1    print(10);    // Calling method 2    print(10.10);    // Calling method 3    print("ten");      return 0;}
 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.
C++-Function Overloading and Default Arguments
C++
CPP
Writing code in comment?
Please use ide.geeksforgeeks.org,
generate link and share the link here.
Operator Overloading in C++
Polymorphism in C++
Friend class and function in C++
Sorting a vector in C++
std::string class in C++
Inline Functions in C++
Pair in C++ Standard Template Library (STL)
Array of Strings in C++ (5 Different Ways to Create)
Convert string to char array in C++
List in C++ Standard Template Library (STL)