function overloading in c
  1. Introduction
  2. Definition of Function Overloading 
  3. Rules 
  4. Example of Function Overloading in C++
  5. Causes of function overloading
  6. Advantages
  7. Disadvantages
  8. Difference between Function Overloading and Operator Overloading
  9. Conclusion

Introduction

C++ has many features, and one of the most important features is function overloading. It is a code that has more than one function with the same name having various types of argument lists. This argument list includes the data type of arguments and the sequence of the arguments.

The function overloading feature is used to improve the readability of the code. It is used so that the programmer does not have to remember various function names. If any class has multiple functions with different parameters having the same name, they are said to be overloaded. If we have to perform a single operation with different numbers or types of arguments, we need to overload the function.

In OOP, function overloading is known as a function of polymorphism. The function can perform various operations best on the argument list. It differs by type or number of arguments they hold. By using a different number of arguments or different types of arguments, the function can be redefined.    

Definition of Function Overloading 

Two or more functions can have the same name but different parameters; such functions are called function overloading.                   

If I say parameter list, it means the data type and sequence of the parameters. For example, the parameters list of a function myfunction(int a, double b) is (int, double), which is different from the function myfunction(double a, int b) parameter list (double, int). Function overloading is a compile-time polymorphism. As we already know what a parameter list is, so let’s see the rules of overloading: we can have the following functions in the same scope.

sum(int a, int b)

sum(int a, int b, int c)

The easiest way to remember this rule is that the parameters should qualify any one or more than one of the following conditions: 

  • they should have a different type
  • they should have a different number 
  • they should have a different sequence of parameters.

Rules 

Different parameters or three different conditions :

1.  These  functions have different parameter type

             sum(int a, int b)

             sum(double a, double b)

2.   These functions have a different number of parameters

             sum(int a, int b)

             sum(int a, int b, int c)

3.  These functions have a different sequence of parameters

              sum(int a, double b)

              sum(double a, int b)

The above three cases are valid cases of overloading. We can have any number of functions, but remember that the parameter list must be different. For example:

int mul(int, int)

double mul(int, int)

As the parameter list is same, this is not allowed. Even though their return types are different, it’s not valid.

Example of Function Overloading in C++

Program:

#include <iostream>
using namespace std;
class Addition
 {
    public:
    int sum(int a,int b) 
    {
        return a+b;
    }
    int sum(int a,int b, int c) 
    {
       return a+b+c;
    }
 };
int main(void)
{
    Addition obj;
    cout<<obj.sum(20, 15)<<endl;
    cout<<obj.sum(81, 100, 10);
    return 0;
}

Output :

Causes of function overloading

1.  Type Conversion.

2.  Function with default arguments.

3.  Function with a pass by reference.

1. Type Conversion

Program :

#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;
}

Output :

This example shows an error “call of overloaded ‘function’ is ambiguous”. According to our prediction, the function(3.4) will call the first function, and the function(34) will call the second function. But this is not what happens because in C++, all the floating-point constants are not treated as float; instead they are treated as double.  If we replace the float variable to a double variable, the program will work properly. Thus we call it a type conversion error from float to double.

2. Function with Default Arguments

Program :

#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;
}

Output :

The above example shows 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:

By calling the function with only one argument (and it will automatically take the value of z = 12)

By calling the function with only two arguments.

When you call the function: function(12), we full fill the condition of both function(int) and function(int, int); therefore, the compiler gets into an ambiguity that gives an error.

3. Function with a Pass by Reference

Program :

#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;
}

Output :

The above program shows 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 cannot understand which function is needed by the user as there is no syntactic difference between the fun(int) and fun(int &); therefore, it shots an error of ambiguity.

By Changing the Number of Arguments

          In this way of function overloading, we define two functions of the same type but with a different number of parameters with the same names. For example, in the program given below, 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 that can accept two arguments and another which can accept three arguments. Which add() function will be called, depends on the number of arguments.

int main() 
{ 
add(10, 20); // add() with 2 parameter     
add(10, 20, 30); //sum() with 3 parameter 
}

Program ( By changing the number of arguments):

#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);
}

Output :

          In the above example, we overload an add() function by changing the number of arguments. First, we define an add() function with the two parameters, and then we can overload it by again defining an 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 parameters of different data types but with the same number of parameters with the same name. For example, in this program, we have three add() functions; the first one will get two integer arguments, the second one will get two float arguments, and the third one will get two double arguments.

Program :

#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);
}

Output :

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

Therefore we override the add() function twice.

Advantages

  • The program is very simple and also easier to understand.
  • It saves the memory space, maintains consistency, makes clear interface for methods regardless of the parameter’s type and readability of the program.
  • Using the function overloading concept, we can develop more than one function with the same name, but the arguments passed should be of different types.
  • Function overloading executes the program faster.
  • Function overloading is used for code reusability and to save memory.

Disadvantages

  • Function declarations that differ by their return type cannot be overloaded with the function overloading process.
  • If any static member function is declared, then the same parameters or the same name types cannot be overloaded.

Difference between Function Overloading and Operator Overloading

Function OverloadingOperator Overloading
Function overloading allows us to call it in multiple ways.Operator overloading allows operators to have their extending meaning beyond its predefined operational meaning.
You can overload function with the same name but with different parameters.You can overload (define custom behaviour) for operators such as ‘+’, ‘-‘, ‘()’, ‘[]’.
Function overloading means using a single name and giving more functionality to it.Operator overloading means adding extra functionality for a certain operator.
When an operator is overloaded, the operator has different meanings, which depends on the type of its operands.When a function is overloaded, the same function name has different interpretations depending on its signature, which is the list of argument types in the functions parameter list.

Conclusion

          Function overloading concept in C++ can be used in different ways to enhance code readability. While doing programming in C++, it helps to save compile time and also saves memory space. You also learned how similar operations are performed using the function overloading concept.

Wondering where to learn the highly coveted in-demand skills for free? Check out the courses on Great Learning Academy.

0

LEAVE A REPLY

Please enter your comment!
Please enter your name here

15 − 14 =