Function overloading in C++ is a feature that allows you to define multiple functions with the same name, but with different parameter lists. This can be useful for defining functions that perform the same basic task, but with different data types or numbers of parameters.
For example, the following code shows two overloaded functions named add():
int add(int x, int y) {
return x + y;
}
double add(double x, double y) {
return x + y;
}
These two functions have the same name, but they have different parameter lists. The first function takes two integers as parameters and returns an integer. The second function takes two doubles as parameters and returns a double.
When you call the add() function, the compiler will choose the appropriate overloaded function based on the data types of the arguments that you pass to the function. For example:
int sum_int = add(10, 20); // Calls the first function
double sum_double = add(1.0, 2.0); // Calls the second function
You can also overload functions with different numbers of parameters. For example, the following code shows two overloaded functions named print():
void print(int value) {
std::cout << value << std::endl;
}
void print(std::string value) {
std::cout << value << std::endl;
}
These two functions have the same name, but they have different numbers of parameters. The first function takes one integer parameter. The second function takes one string parameter.
When you call the print() function, the compiler will choose the appropriate overloaded function based on the data type of the argument that you pass to the function. For example:
print(10); // Calls the first function
print("Hello, world!"); // Calls the second function
Function overloading is a powerful feature that can make your code more reusable and maintainable. By understanding how to use function overloading, you can write more efficient and effective C++ code.
Here is another example of function overloading:
// Overloaded functions for calculating the area of different shapes.
double area(Rectangle rectangle) {
return rectangle.length * rectangle.width;
}
double area(Circle circle) {
return circle.radius * circle.radius * M_PI;
}
// Overloaded functions for calculating the volume of different shapes.
double volume(Cuboid cuboid) {
return cuboid.length * cuboid.width * cuboid.height;
}
double volume(Sphere sphere) {
return 4.0 / 3.0 * M_PI * sphere.radius * sphere.radius * sphere.radius;
}
int main() {
Rectangle rectangle = {10.0, 5.0};
Circle circle = {5.0};
Cuboid cuboid = {10.0, 5.0, 2.0};
Sphere sphere = {5.0};
// Calculate the areas of the rectangle and circle.
double rectangle_area = area(rectangle);
double circle_area = area(circle);
// Calculate the volumes of the cuboid and sphere.
double cuboid_volume = volume(cuboid);
double sphere_volume = volume(sphere);
// Print the results.
std::cout << "Rectangle area: " << rectangle_area << std::endl;
std::cout << "Circle area: " << circle_area << std::endl;
std::cout << "Cuboid volume: " << cuboid_volume << std::endl;
std::cout << "Sphere volume: " << sphere_volume << std::endl;
return 0;
}
Output:
Rectangle area: 50.0
Circle area: 78.5398
Cuboid volume: 100.0
Sphere volume: 523.6