Introduction to Method Overloading in C++
Method overloading is the process of overloading the method that has the same name but different parameters. C++ provides this method of overloading features. Method overloading allows users to use the same name to another method, but the parameters passed to the methods should be different. The return type of methods can be the same or different. In this article, we are going to 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 to implement 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. Here 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. 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 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. Here adMethod is 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:
Here the compiler counts float numbers as an argument because the return type and the argument type mentioned in the method is an integer. So, it will treat float as an integer.
Example #3
Here 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:
Here the compiler will throw an error because the types of arguments and the number of arguments passed to methods 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 in the programming.
- Method overloading can also be used in constructors, to initialize objects using different methods.
- It allows methods that perform related functions to be accessed using a common name with a slight difference in the return type and a number of 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++ along with code implementation. You can also go through our other suggested articles to learn more –
4 Online Courses | 5 Hands-on Projects | 37+ Hours | Verifiable Certificate of Completion
4.5
View Course
Related Courses