Loading...
C++ Function Overloading

C++ Function Overloading

In this tutorial, you will learn about Function Overloading, Types of function overloading with the help of examples.

Function Overloading

  • Overloading means the use of same thing for different purposes.
  • Function overloading is a most important feature in C++ where two or more functions can have the same name but different parameters or arguments for the different tasks.
  • Function overloading can be considered as an example of polymorphism feature in C++.
  • The function will perform different operations but on the basis of the argument list in the function call.
  • Function Overloading is usually used to enhance the readability of the program.

Syntax

These functions having the same name but different arguments are known as overloaded functions. For example:

// same name different arguments
int test() { }
int test(int a) { }
int test(int a, double b) { }
float test(double a) { }
double test(double a) { }
int test(int a, double b) { }

Here, all 6 functions are overloaded functions.

Notice that the return types of all these 6 functions are not the same.

Overloaded functions may or may not have different return types but they must have different arguments. For example,

// Error code
int test(int a) { }
double test(int b){ }

Here, both functions have the same name, the same type, and the same number of arguments. Hence, the compiler will throw an error.


Different ways to Overloading a Function

  1. By having different types of Arguments/Parameters.
  2. By changing number of Arguments/Parameters.

Function Overloading using Different Types of Arguments/Parameters

  • In this method, we define two or more functions with same name and same number of parameters but the data type of parameter is different.
  • For example, we have two add() function, first one gets two integer arguments and second one gets two double arguments.

Example 1: C++ Function Overloading using Different Types of Arguments/Parameters

// Program to compute sum of value
// Works for both int and double
                                      
#include <iostream>
using namespace std;
                                      
// first definition                               
int add(int a, int b) 
{
    cout  << "Sum of a + b (integer value) = " <<  a + b << endl;
}
                                      
// Second overloaded definition                               
double add(double a, double b) 
{
    cout  << "Sum of a + b (floating point value) = "  <<  a + b << endl;
}
                                      
int main() {
                                          
    add (1, 2);
    add (1.5, 2.5);
                                      
    return 0;
}

Output

Sum of a + b (integer value) = 3
Sum of a + b (floating point value) = 4

In this program, we overload the add() function. Based on the type of parameter passed during the function call, the corresponding function is called.


Function Overloading using Different Number of Arguments/Parameters

  • In this type of function overloading we define two functions with same names but different number of parameters of the same type.
  • For example, in the below program we have made two add() functions to return sum of two and three integers.

Example 2: C++ Function Overloading using Different Numbers of Arguments/Parameters

// Program to compute sum of value                                      
#include <iostream>
using namespace std;
                                      
// first definition                               
int add(int a, int b) 
{
    cout << "Sum of a + b (integer value) = " <<  a + b << endl;
}
                                      
// Second overloaded definition                               
int add(int a, int b, int c) 
{
    cout  << "Sum of a + b + c (floating point value) = "  << a + b + c << endl;
}
                                      
int main() {
                                          
    add (1, 2);
    add (1.5, 2.5, 3.5);
                                      
    return 0;
}

Output

Sum of a + b (integer value) = 3
Sum of a + b + c (floating point value) = 6

Here, the add() function is called two times with different arguments. Depending on the number and type of arguments passed, the corresponding add() function is called.

The return type of all these functions is the same but that need not be the case for function overloading.


Note: In C++, many standard library functions are overloaded. For example, the sqrt() function can take double, float, int, etc. as parameters. This is possible because the sqrt() function is overloaded in C++.


Advantages of Function Overloading

  • Function overloading save the memory space, consistency and readability of our program.
  • We can develop more then one function with the same name.
  • Function overloading shows the behaviour of polymorphism that allows us to get different behaviour, although there will be some link using th esame name of the function.
  • Function overloading speeds up the execution of the program.
  • It is used for code reusability and also to save memory.
  • It helps application to load the class method based on the type of parameter.
  • Code maintenance is easy

Disadvantages of Function Overloading

  • Function declarations that differ only by its return type cannot be overloaded with function overloading process.
  • Member function declarations with the same parameters or the same name types cannot be overloaded if any one of them is declared as a static member function.

Next Tutorial

We hope that this tutorial helped you develop better understanding of the concept of Function Overloading in C++.

Keep Learning : )

In the next tutorial, you'll learn about C++ Function Overriding.

- Related Topics