Function Overloading in C++ : All you Need to Know

Last updated on Sep 25,2019 29.1K Views


C++ is one of those programming languages that is very flexible and it also covers several features of object-oriented programming. Overloading is another feature of this programming language. Following Pointers will be covered in this “Function Overloading in C++” article-

C++ Overloading

When we create two or more members of a class having the same name but different in number or type of parameters, it is known as C++ overloading. In C++, we can overload:

  • methods,
  • constructors, and
  • indexed properties

 

Types of overloading in C++

types-of-overloading-in-c++

 

What is function overloading in C++?

Function Overloading in C++ can be defined as the process of having two or more member functions of a class with the same name, but different in parameters. In function overloading, the function can be redefined either by using different types of arguments or a different number of arguments according to the requirement. It is only through these differences compiler can differentiate between the two overloaded functions.

One of the major advantages of Function overloading is that it increases the readability of the program because we don’t need to use different names for the same action again and again.

 

By changing the Number of Arguments

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

// first function definition
int add(int a, int b)
{
    cout << a+b;
}

// second overloaded function definition
int add(int a, int b, int c)
{
    cout << a+b+c;
}

Here add() function is said to be overloaded, as it has two definitions, one which accepts two arguments and another which accepts three arguments. Which add() function will be called, depends on the number of arguments.

int main()
{
    add(10, 20);  // add() with 2 parameter will be called

    add(10, 20, 30);  //sum() with 3 parameter will be called

}
#include <iostream>

using namespace std;

int add(int a, int b)
{
    cout << a+b <<endl;
    return 0;
}

int add(int a, int b, int c)
{
    cout << a+b+c <<endl;
    return 0;
}

int main()
{
    
    add(20, 40);  

    add(40, 20, 30);  
}

function-overloading-in-c++-changing-no-of-arguments

In the above example, we overload add() function by changing its number of arguments. First, we define an add() function with two parameters, then we overload it by again defining the add() function but this time with three parameters.

 

By having different types of Arguments

In this method, we define two or more functions with the same name and the same number of parameters, but the data type used for these parameters are different. For example in this program, we have three add() function, the first one gets two integer arguments, the second one gets two float arguments and the third one gets two double arguments.

#include <iostream>

using namespace std;
int add(int x, int y) // first definition
{
    cout<< x+y << endl;

    return 0;
}

float add(float a, float b)
{
	cout << a+b << endl;
	return 0;
}


double add(double x, double y)
{
    cout << x+y << endl;
    return 0;
}

int main()
{
    
    add(20, 40);  

    add(23.45f, 34.5f);

    add(40.24, 20.433);  

}

function-overloading-in-c++-different-types-of-arguments

In the above example, we define add() function three times. First using integers as parameters, second using float as parameters and third using double as a parameter.
Thus we override the add() function twice.

 

Advantages of function Overloading in C++

  • We use function overloading to save the memory space, consistency, and readability of our program.

  • With the use function overloading concept, we can develop more than one function with the same name

  • Function overloading shows the behavior of polymorphism that allows us to get different behavior, although there will be some link using the same name of the function.

  • Function overloading speeds up the execution of the program.

  • Function overloading 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 in C++

  • 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.
  • class XYZ{
      static void func();
      void func(); // error
      };

 

Function Overloading and Ambiguity

When the compiler is unable to decide which function it should invoke first among the overloaded functions, this situation is known as function overloading ambiguity. The compiler does not run the program if it shows ambiguity error. Causes of Function Overloading ambiguity:

  • Type Conversion.
  • Function with default arguments.
  • Function with a pass by reference

 

Type Conversion:

#include<iostream>  
using namespace std;  

void function(float); 
void function(int);  

void function(float x)  
{  
    std::cout << "Value of x is : " <<x<< std::endl;  
}  

void function(int y)  
{  
    std::cout << "Value of y is : " <<y<< std::endl;  
}  

int main()  
{  
    function(3.4);
    function(34);  
    return 0;  
}

type-conversion

The above example throws an error – “call of overloaded ‘function(double)’ is ambiguous”. The function(3.4) will call the first function. The function(34) calls the second function according to our prediction. But this is not what happens because in C++ all the floating-point constants are treated as double not as a float. If we replace the float variable to a double variable, the program will work fine. Therefore we call this a type conversion error from float to double.

 

Function with Default Arguments:

#include<iostream>  
using namespace std;  
void function(int);  
void function(int,int);  

void function(int x)  
{  
    std::cout << "Value of x is : " <<x<< std::endl;  
}  
void function(int y,int z=12)  
{  
    std::cout << "Value of y is : " <<y<< std::endl;  
    std::cout << "Value of z is : " <<z<< std::endl;  
}  
int main()  
{  
    function(12);  
   
    return 0;  
}

function-overloading-in-c++-function-default-arguments

The above example gives an error saying “call of overloaded ‘fun(int)’ is ambiguous”, this is because function(int y, int z=12) can be called in two ways:

  1. By calling the function with one argument (and it will automatically take the value of z = 12)
  2. By calling the function with two arguments.

When we call the function: function(12) we full fill the condition of both function(int) and function(int, int) thus the compiler gets into an ambiguity shows an error.

 

Function with pass by reference

#include <iostream>  
using namespace std;  
void function(int);  
void function(int &);   

void function(int a)  
{  
std::cout << "Value of a is : " <<a<< std::endl;  
}  
void function(int &b)  
{  
std::cout << "Value of b is : " <<b<< std::endl;  
}  

int main()  
{  
int x=10;  
function(x); 
return 0;  
}

pass-by-reference

The above program gives an error saying “call of overloaded ‘fun(int&)’ is ambiguous”. As we see the first function takes one integer argument and the second function takes a reference parameter as an argument. In this case, the compiler is not able to understand which function is needed by the user as there is no syntactical difference between the fun(int) and fun(int &) thus it shots an error of ambiguity.

With this, we come to an end of this Function Overloading in C++. If you wish to learn more, check out the Java Training by Edureka, a trusted online learning company. Edureka’s Java J2EE and SOA training and certification course is designed to train you for both core and advanced Java concepts along with various Java frameworks like Hibernate & Spring.

Got a question for us? Please mention it in the comments section of this blog and we will get back to you as soon as possible.

Comments
0 Comments

Join the discussion

Browse Categories

Subscribe to our Newsletter, and get personalized recommendations.