38. Function Overloading

By | September 24, 2021

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

The advantage of Function overloading is that it increases the readability of the program because you don’t need to use different names for the same action.

Example

#include <iostream>
using namespace std;
 
class printData {
   public:
      void print(int i) {
        cout << "Printing int: " << i << endl;
      }
      void print(double  f) {
        cout << "Printing float: " << f << endl;
      }
      void print(char* c) {
        cout << "Printing character: " << c << endl;
      }
};

int main(void) {
   printData pd;
 
   // Call print to print integer
   pd.print(5);
   
   // Call print to print float
   pd.print(500.263);
   
   // Call print to print character
   pd.print("Hello C++");
 
   return 0;
}

When the above code is compiled and executed, it produces the following result −

Printing int: 5
Printing float: 500.263
Printing character: Hello C++

Function Overloading and Ambiguity

When the compiler is unable to decide which function is to be invoked among the overloaded function, this situation is known as function overloading.

When the compiler shows the ambiguity error, the compiler does not run the program.

Causes of Function Overloading:

  • Type Conversion.
  • Function with default arguments.
  • Function with pass by reference.
C++ Overloading

01. Type Conversion:

Let’s see a simple example.

#include<iostream>  
using namespace std;  
void fun(int);  
void fun(float);  
void fun(int i)  
{  
    std::cout << “Value of i is : ” <<i<< std::endl;  
}  
void fun(float j)  
{  
    std::cout << “Value of j is : ” <<j<< std::endl;  
}  
int main()  
{  
    fun(12);  
    fun(1.2);  
    return 0;  
}  

The above example shows an error “call of overloaded ‘fun(double)’ is ambiguous“. The fun(10) will call the first function. The fun(1.2) calls the second function according to our prediction. But, this does not refer to any function as in C++, all the floating point constants are treated as double not as a float. If we replace float to double, the program works. Therefore, this is a type conversion from float to double.

02. Function with Default Arguments

Let’s see a simple example.
#include<iostream>  
using namespace std;  
void fun(int);  
void fun(int,int);  
void fun(int i)  
{  
    std::cout << “Value of i is : ” <<i<< std::endl;  
}  
void fun(int a,int b=9)  
{  
    std::cout << “Value of a is : ” <<a<< std::endl;  
    std::cout << “Value of b is : ” <<b<< std::endl;  
}  
int main()  
{  
   fun(12);    
    return 0;  
}  

The above example shows an error “call of overloaded ‘fun(int)’ is ambiguous”. The fun(int a, int b=9) can be called in two ways: first is by calling the function with one argument, i.e., fun(12) and another way is calling the function with two arguments, i.e., fun(4,5). The fun(int i) function is invoked with one argument. Therefore, the compiler could not be able to select among fun(int i) and fun(int a,int b=9).

03. Function with pass by reference

Let’s see a simple example.
#include <iostream>  
using namespace std;  
void fun(int);  
void fun(int &);   
int main()  
{  
int a=10;  
fun(a); // error, which f()?  
return 0;  
}  
void fun(int x)  
{  
std::cout << “Value of x is : ” <<x<< std::endl;  
}  
void fun(int &b)  
{  
std::cout << “Value of b is : ” <<b<< std::endl;  
}  

The above example shows an error “call of overloaded ‘fun(int&)’ is ambiguous“. The first function takes one integer argument and the second function takes a reference parameter as an argument. In this case, the compiler does not know which function is needed by the user as there is no syntactical difference between the fun(int) and fun(int &).

Leave a Reply

Your email address will not be published. Required fields are marked *