Introduction to Method Overloading in C++
Method overloading is the process of overloading a method with the same name but different parameters. C++ provides this method of overloading features. Method overloading allows users to use the same name as another, but the parameters passed to the methods should differ. The return type of methods can be the same or different. This article will discuss the method overloading in C++ with its working and examples.
Syntax:
int sample(a)
{
}
int sample(int a , int b)
{
}
float sample(float a, float b)
{
}
Here the sample is the name of the method. This method has different arguments. Return types used for these methods are different. We can use the same or different return types.
Examples to Implement Methods Overloading in C++
Let us see some examples of implementing method overloading in C++, which are given below:
Example #1
Program to implement the method overloading with a different number of arguments.
Code:
#include <iostream>
using namespace std;
class addition
{
public:
int addMethod(int x, int y)
{
return x + y;
}
int addMethod(int x, int y, int z)
{
return x + y + z;
}
};
int main(void)
{
addition add;
cout << add.addMethod(2, 3) << endl;
cout << add.addMethod(2, 3, 6) << endl;
return 0;
}
Output:
Here we have written a program to implement method overloading with a different number of arguments. In this case, “adMethod” is the name of the methods used for overloading the method. Here we have used the same return type for both methods. The program will perform the addition operation. The first method passes two arguments to the adMethod. This method will add two integer numbers and print the result. The second method passes three arguments to the adMethod. This will add three integer numbers and will print the result.
Example #2
Program to implement the method overloading with a different number of arguments and different return types.
Code:
#include <iostream>
using namespace std;
class addition
{
public:
int addMethod(int x, int y)
{
return x + y;
}
float addMethod(float x, float y, float z)
{
return x + y + z;
}
};
int main(void)
{
addition add;
cout << add.addMethod(2, 3) << endl;
cout << add.addMethod(2.2, 3.3, 6.1) << endl;
return 0;
}
Output:
Here we have written a program to implement method overloading with a different number of arguments and different return types. In this case, “adMethod” serves as the name of the methods used for overloading the method. Here we have used the different return types for both methods. The program will perform the addition operation. In the first method, two arguments are passed. This method will add two integer numbers and print the result. In the second method, three arguments are passed. This will add three floating numbers and will print the result.
Code:
#include <iostream>
using namespace std;
class addition
{
public:
int addMethod(int x, int y)
{
return x + y;
}
float addMethod(float x, float y, float z)
{
return x + y + z;
}
};
int main(void)
{
addition add;
cout << add.addMethod(2, 3) << endl;
cout << add.addMethod(2.2, 3.3, 6.1) << endl;
return 0;
}
Output:
In this case, the compiler considers float numbers as arguments because the return type and argument type specified in the method are integers. So, it will treat float as an integer.
Example #3
If we try to pass the float numbers to the int return type, it will not accept the argument and will throw an error.
Code:
#include <iostream>
using namespace std;
class addition
{
public:
int addMethod(int x, int y)
{
return x + y;
}
float addMethod(int x, int y)
{
return x + y;
}
};
int main(void)
{
addition add;
cout << add.addMethod(2, 3) << endl;
cout << add.addMethod(21, 3) << endl;
return 0;
}
Output:
The compiler will throw an error if the types and the number of arguments passed to methods in method overloading are the same.
Advantages of Method Overloading in C++
Below are some of the advantages mentioned.
- Method overloading increases the readability of the program.
- It also increases the efficiency of the programming.
- Utilizing method overloading, constructors can initialize objects through various methods.
- Users can access methods that perform related functions using a common name, with slight differences in the return type and several arguments.
Recommended Articles
This is a guide to Method Overloading in C++. Here we discuss the basic concept and examples of Method Overloading in C++ and code implementation. You can also go through our other suggested articles to learn more –