C++ Program to Perform function overloading in C ++

In this example program, we are discussing a simple example of function overloading where we are changing the number of arguments f add() method.

What is Function overloading in C++?

Function Overloading is defined as the process of having two or more functions with the same name, but different parameters are 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.

C++ program of function overloading when the number of arguments varies.

Create a class cal and define a method add with 2 variables a and b as arguments. Return a value of a+b to the main function.
static int add(int a,int b){      
        return a + b;      
    }      

define another method add with 3 variables a , b, and c as arguments. Retun a value of a+b+c to the main function.
static int add(int a,int b,int c){      
        return a + b + c;      
    }      

Create an object obj for class cal. And call the function to add (a,b) with object obj.
obj.add(10, 20)
This statement will invoke the function static int add(int a, int b) and return a value a+b.
call the function add (a,b, c) with object obj.
obj.add(12, 23, 20)
This statement will invoke the function static int add(int a,int b, int c) and return a value a+b+c. Display the result on the screen.

Algorithm

Step 1:  Call the header file iostream.

Step 2: Use the namespace std.

Step 3: Create a class cal.

Step 4: define a function with two arguments and return a value of the sum of the arguments.

Step 5: Define the same function with three arguments and  return a value of the sum of the arguments

Step 6: call the function main

Step 7: create an object obj for the class cal.

Step 8: Call the function with the object and pass two integers as arguments.

Step 9: Call the function with the object and pass three integers as arguments

Step 10: Display the result on the screen.

Step 11: Exit 

C++ Source Code

                                          #include <iostream>    
using namespace std;    
class Cal {    
    public:    
static int add(int a,int b){      
        return a + b;      
    }      
static int add(int a, int b, int c)      
    {      
        return a + b + c;      
    }      
};     
int main(void) {    
    Cal obj;                                                    //     class object declaration.   
    cout<<obj.add(10, 20)<<endl;      
    cout<<obj.add(12, 20, 23);     
   return 0;    
}    
                                      

OUTPUT

30
55