Function Overloading in C++

0
310

If any class have multiple functions with the same names but different parameters then they are said to be overloaded. Function overloading allows you to use the same name for different functions, to perform, either the same or different functions in the same class. Function overloading is usually used to enhance the readability of the program. If you have to perform one single operation but with a different number or types of arguments, then you can simply overload the function.

Ways to overload a function:
1. By changing the number of Arguments.
2. By having different types of argument.

By changing number of Arguments: 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 mentioned program we have made two sum() functions to return sum of two and three integers.

#include <iostream>
using namespace std;
int sum (int x, int y)
{
 cout << x+y;
}
int sum(int x, int y, int z)
{
 cout << x+y+z;
}
int main()
{
sum (10,20);  // sum() with 2 parameter will be called
sum(10,20,30);  //sum() with 3 parameter will be called
}

Different Datatype of Arguments: In this type of overloading we define two or more functions with the same name and the same number of parameters, but the type of parameter is different. For example in this program, we have two sum() function, first one gets two integer arguments and the second one gets two double arguments.

#include <iostream>
using namespace std;
int sum(int x,int y)
{
 cout<< x+y;
}
double sum(double x,double y)
{
 cout << x+y;
}
int main()
{
 sum (10,20);
 sum(10.5,20.5);
}

Default Arguments: When we mention a default value for a parameter while declaring the function, it is said to be as the default argument. In this case, even if we make a call to the function without passing any value for that parameter, the function will take the default value specified.

#include <iostream>
using namespace std;
void sum(int x,int y= 50)
{
cout << x+y;
}
int main()
{
sum(10);
sum(10,0);
sum(10,10);
}

Rules for using Default Arguments:
1. Only the last argument must be given default value. You cannot have a default argument followed by non-default argument.
sum (int x,int y);
sum (int x,int y=0);
sum (int x=0,int y); // This is Incorrect
2. If you default an argument, then you will have to default all the subsequent arguments after that.
sum (int x,int y=0);
sum (int x,int y=0,int z); // This is incorrect
sum (int x,int y=10,int z=10); // Correct

Placeholder Arguments:
1. When arguments in a function are declared without any identifier they are called placeholder arguments.
void sum (int,int);
Such arguments can also be used with default arguments.
void sum (int, int=0);

Call by reference:

#include <iostream>
using namespace std;
void swap(int &x, int &y);
int main() 
{ 
   int a = 100;
   int b = 200;
   cout << "Before swap, value of a :" << a << endl;
   cout << "Before swap, value of b :" << b << endl;
   swap(a, b);
   cout << "After swap, value of a :" << a << endl;
   cout << "After swap, value of b :" << b << endl;
   return 0;
}
void swap(int &x, int &y) 
{
   int temp;
   temp = x; 
   x = y;    
   y = temp; 
   return;
}

Call by value:

#include <iostream>
using namespace std;
void swap(int x, int y);
int main () 
{
   int a = 100;
   int b = 200; 
   cout << "Before swap, value of a :" << a << endl;
   cout << "Before swap, value of b :" << b << endl;
   swap(a, b);
   cout << "After swap, value of a :" << a << endl;
   cout << "After swap, value of b :" << b << endl;
   return 0;
}
void swap(int x, int y) 
{
   int temp;
   temp = x; 
   x = y;    
   y = temp; 
   return;
}

LEAVE A REPLY

Please enter your comment!
Please enter your name here