Learnerslesson
   JAVA   
  SPRING  
  SPRINGBOOT  
 HIBERNATE 
  HADOOP  
   HIVE   
   ALGORITHMS   
   PYTHON   
   GO   
   KOTLIN   
   C#   
   RUBY   
   C++   




C++ - FUNCTION OVERLOADING


Function Overloading says, when two or more functions have the same name but different parameters.


Below are two types of difference in parameters that C++ considers for function overloading :

  1. The number of parameters are different in two or more functions.

  2. The number of parameters are same in two or more functions but have different datatypes.

Now, let's see the first way of operator Overloading.

  1. The number of parameters are different in two or more functions.

    1. Say there is an add function with three parameters :

      int add(int firstNumber, int secondNumber, int thirdNumber)

    2. Also there is an add function with two parameters :

      int add(int firstNumber, int secondNumber)

    3. Also, there is an add function with just one parameter :

      int add(int firstNumber)


    All the above three functions are said to be overloaded functions and C++ would treat them as separate functions.

    So, let us try adding 2 numbers with add function. Then try adding 3 numbers with the overloaded add function.

    Example :



    #include <iostream>
    using namespace std;
    
    //Add function starts here.
    int add(int firstNumber, int secondNumber) {
    
    	int result;
    	result = firstNumber + secondNumber;
    	return result;
    }
    
    //Overloaded Add function starts here.
    int add(int firstNumber, int secondNumber, int thirdNumber)
    {
    	int result;
    	result = firstNumber + secondNumber + thirdNumber;
    	return result;
    }
    	
    int main()
    {
    	int firstNum = 5;
    	int secondNum = 8;
    	int thirdNum = 2;
    	int addedResult1;
    	int addedResult2;
    
    	addedResult1 = add(firstNum,secondNum);
    
    	cout << "The added value of two numbers is : " << addedResult1 << endl;
    	
    	addedResult2 = add(firstNum,secondNum,thirdNum);
    	
    	cout << "The added value of three numbers is : " << addedResult2 << endl;
    	
    	return 0;
    }
    


    Output :



      The added value of two numbers is : 13
      The added value of three numbers is : 15


    In the main function, at first, we have called the add function with two parameters :

    addedResult1 = add(firstNum,secondNum);


    Then we have called the add function with three parameters :

    addedResult2 = add(firstNum,secondNum,thirdNum);


    And C++ treated both the functions as different functions, since they were overloaded.

    Next, let us look at the second way of Operator Overloading.

  2. The number of parameters are same in two or more functions but have different datatypes.

    1. Say there is an add function with two parameters :

      float add(float firstNumber, float secondNumber)


    2. Also there is an add function with two parameters :

      int add(int firstNumber, int secondNumber)


    Since, the datatype of the parameters in two functions do not match(i.e. Float and Int). The functions are said to be overloaded and C++ would treat them as separate functions.

    Example :



    #include <iostream>
    using namespace std;
    
    //Add function starts here.
    int add(int firstNumber, int secondNumber) {
    
    	int result;
    	result = firstNumber + secondNumber;
    	return result;
    }
    
    //Overloaded Add function starts here.
    float add(float firstNumber, float secondNumber)
    {
    	float result;
    	result = firstNumber + secondNumber;
    	return result;
    }
    	
    int main()
    {
    	int firstNum = 5;
    	int secondNum = 8;
    	
    	float firstNumFloat = 2.4;
    	float secondNumFloat = 9.5;
    	
    	int addedResult1;
    	float addedResult2;
    
    	addedResult1 = add(firstNum,secondNum);
    
    	cout << "The added value of two numbers is : " << addedResult1 << endl;
    	
    	addedResult2 = add(firstNumFloat,secondNumFloat);
    	
    	cout << "The added value of decimal numbers : " << addedResult2 << endl;
    	
    	return 0;
    }
    


    Output :



      The added value of two numbers : 13
      The added value of decimal numbers : 11.9


    So, in the above code we have two functions with the same name but parameters are of different Data Types.

    int add(int firstNumber, int secondNumber)


    And

    float add(float firstNumber, float secondNumber)


    So, when the function is called with two Integer values,

    addedResult1 = add(firstNum,secondNum);


    The function with two Int parameteres is called,

    int add(int firstNumber, int secondNumber)


    Similarly, when the function is called with two Float values,

    addedResult2 = add(firstNumFloat,secondNumFloat);


    The function with two Float parameteres is called,

    float add(float firstNumber, float secondNumber)