Tech Study

Function Overloading in C++

Introduction :

Function Overloading in C++

In C++ two or more functions are allows to have the same name but are supposed to have different parameters; such functions make examples of function overloading in c++

As we well aware of what a parameter list is, so we can see the rules or conditions for overloading: we are allowed to have the following functions in the same scope.

rishsum(int ri, int sh)

rishsum(int ri, int sh, int ve)

The easiest way for remembering the above rule is that the parameters are required to qualify any one or more than one of the following conditions: 

  • They are supposed to have a different type
  • They are supposed to have a different number 
  • They are supposed to have a different sequence of parameters.

Function overloading in C++ are allowed to have the same name but different parameters

Example 1:

#include <iostream>

using namespace std;




void rishSumNum(int A, int B);

void rishSumNum(int A, int B, int C);

void rishSumNum(int A, int B, int C, int D);




int main()

{

    rishSumNum(1,2);

    rishSumNum(1,2,3);

    rishSumNum(1,2,3,4);

    

    return 0;

}




void rishSumNum(int A, int B)

{

     cout<< endl << "riSUMNUM is : "<< A+B;     

}




void rishSumNum(int A, int B, int C)

{

     cout<< endl << "riSUMNUM is : "<< A+B+C;  

}




void rishSumNum(int A, int B, int C, int D)

{

     cout<< endl << "riSUMNUM is : "<< A+B+C+D;     

}

Output of the above code:

riSUMNUM is :3

riSUMNUM is :6

riSUMNUM is :10

Example 2:

#include <iostream>

using namespace std;

class Addition

 {

    public:

    int rishsum(int a,int b) 

    {

        return a+b;

    }

    int rishsum(int a,int b, int c) 

    {

       return a+b+c;

    }

 };

int main(void)

{

    Addition obj;

    cout<<obj.rishsum(22, 15)<<endl;

    cout<<obj.rishsum(82, 100, 10);

    return 0;

}

function overloading in c++

Output of the above code :

37

192

With the same name of the function, function overloading can be considered to be similar to polymorphism that could help us to get different behaviour. Function overloading in c++ is generally used for the purpose of code reusability and to save memory.

We have certain rules for function overloading:-

Below mentioned functions are having different parameter type

  1. risum(int a, int b)
  2. risum(double a, double b)
  3.   Below mentioned functions are having a different number of parameters
  4. risum(int a, int b)
  5. risum(int a, int b, int c)
  6. Below mentioned functions are having a different sequence of parameters
  7. risum(int a, double b)
  8. risum(double a, int b)

The above three cases can be considered to be valid cases of overloading. The parameter list must be different if are having many number of functions. For example:

int rimul(int, int)

double rimul(int, int)

As the parameter list is the same in above mentioned functions, such declaration is not allowed. Even though their return types are different, it will not be considered to be valid.

function overloading in c++

Java Final keyword

Introduction : java final keyword The final keyword present in Java programming language is generally used for restricting the user. …

Read more

C++ Memory Management: new and delete

C++ Memory Management We know that arrays store contiguous and the same type of memory blocks, so memory is allocated …

Read more