Open In App

Advantages and Disadvantages of Function Overloading in C++

Last Updated : 23 Mar, 2021
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.



Previous Article
Next Article

Similar Reads

Advantages and Disadvantages of OOP
OOP stands for Object-Oriented Programming. As you can guess from it's name it breaks the program on the basis of the objects in it. It mainly works on Class, Object, Polymorphism, Abstraction, Encapsulation and Inheritance. Its aim is to bind together the data and functions to operate on them. Some of the well-known object-oriented languages are O
3 min read
Advantages and Disadvantages of C++
C++ is a general-purpose programming language and widely used nowadays for competitive programming. It has imperative, object-oriented, and generic programming features. C++ runs on lots of platforms like Windows, Linux, Unix, Mac, etc. But there are the benefits and drawbacks of C++ on the idea of which they will start their new journey of program
4 min read
error: call of overloaded ‘function(x)’ is ambiguous | Ambiguity in Function overloading in C++
Pre-requisite: Function Overloading in C++ Function overloading is a feature of object-oriented programming where two or more functions can have the same name but different parameters. When a function name is overloaded with different jobs it is called Function Overloading. Two or more functions are said to be overloaded if they differ in any of th
9 min read
Function Overloading vs Function Overriding in C++
Function Overloading (achieved at compile time) Function Overloading provides multiple definitions of the function by changing signature i.e. changing number of parameters, change datatype of parameters, return type doesn’t play any role. It can be done in base as well as derived class.Example: void area(int a); void area(int a, int b); C/C++ Code
3 min read
C++ | Function Overloading and Default Arguments | Question 5
Which of the following in Object Oriented Programming is supported by Function overloading and default arguments features of C++. (A) Inheritance (B) Polymorphism (C) Encapsulation (D) None of the above Answer: (B) Explanation: Both of the features allow one function name to work for different parameter. Quiz of this Question
1 min read
C++ | Function Overloading and Default Arguments | Question 2
Output? #include&lt;iostream&gt; using namespace std; int fun(int x = 0, int y = 0, int z) { return (x + y + z); } int main() { cout &lt;&lt; fun(10); return 0; } (A) 10 (B) 0 (C) 20 (D) Compiler Error Answer: (D) Explanation: All default arguments must be the rightmost arguments. The following program works fine and produces 10 as output. #include
1 min read
C++ | Function Overloading and Default Arguments | Question 3
Which of the following overloaded functions are NOT allowed in C++? 1) Function declarations that differ only in the return type int fun(int x, int y); void fun(int x, int y); 2) Functions that differ only by static keyword in return type int fun(int x, int y); static int fun(int x, int y); 3)Parameter declarations that differ only in a pointer * v
1 min read
C++ | Function Overloading and Default Arguments | Question 4
Predict the output of following C++ program. include&lt;iostream&gt; using namespace std; class Test { protected: int x; public: Test (int i):x(i) { } void fun() const { cout &lt;&lt; &quot;fun() const &quot; &lt;&lt; endl; } void fun() { cout &lt;&lt; &quot;fun() &quot; &lt;&lt; endl; } }; int main() { Test t1 (10); const Test t2 (20); t1.fun(); t
1 min read
C++ | Function Overloading and Default Arguments | Question 5
Output of following program? #include &lt;iostream&gt; using namespace std; int fun(int=0, int = 0); int main() { cout &lt;&lt; fun(5); return 0; } int fun(int x, int y) { return (x+y); } (A) Compiler Error (B) 5 (C) 0 (D) 10 Answer: (B) Explanation: The statement "int fun(int=0, int=0)" is declaration of a function that takes two arguments with de
1 min read
Function Overloading and Return Type in C++
Function overloading is possible in C++ and Java but only if the functions must differ from each other by the types and the number of arguments in the argument list. However, functions can not be overloaded if they differ only in the return type. Why is Function overloading not possible with different return types? Function overloading comes under
2 min read
Practice Tags :