Function overloading is a feature in C++ that allows multiple functions with the same name but different parameter lists to coexist within the same scope. This enables you to define multiple functions with the same name, each tailored to accept different types or numbers of parameters. Function overloading is a powerful technique for enhancing code readability, flexibility, and reusability.
Function overloading allows multiple functions with the same name to exist within the same scope, differentiated by their parameter lists. Let’s delve deeper into this topic with code examples, outputs, and explanations
#include
// Function to calculate the sum of two integers
int add(int a, int b) {
return a + b;
}
// Overloaded function to calculate the sum of three integers
int add(int a, int b, int c) {
return a + b + c;
}
int main() {
std::cout << "Sum of 2 and 3: " << add(2, 3) << std::endl;
std::cout << "Sum of 2, 3, and 4: " << add(2, 3, 4) << std::endl;
return 0;
}
// output //
Sum of 2 and 3: 5
Sum of 2, 3, and 4: 9
add
with different parameter lists. The first add
function accepts two integer parameters, while the second add
function accepts three integer parameters. These functions are overloaded because they have the same name but different parameter lists.main
function, we demonstrate calling both overloaded functions. The first call add(2, 3)
invokes the version of add
that takes two integers, resulting in the sum of 2 and 3, which is 5. The second call add(2, 3, 4)
invokes the version of add
that takes three integers, resulting in the sum of 2, 3, and 4, which is 9.In this section, we demonstrated function overloading with different parameter types. Let’s see the code snippet and its output:
#include
// Function to calculate the sum of two integers
int add(int a, int b) {
return a + b;
}
// Overloaded function to concatenate two strings
std::string add(std::string str1, std::string str2) {
return str1 + str2;
}
int main() {
std::cout << "Sum of 2 and 3: " << add(2, 3) << std::endl;
std::cout << "Concatenation of 'Hello' and 'World': " << add("Hello", "World") << std::endl;
return 0;
}
// output //
Sum of 2 and 3: 5
Concatenation of 'Hello' and 'World': HelloWorld
add
function to accept both integers and strings.main
, we called the add
function with both integer and string arguments.add
function is invoked based on the argument types, resulting in the correct behavior.In this section, we showcased function overloading based on different const qualifiers.
#include
// Function to calculate the square of an integer
int square(int x) {
return x * x;
}
// Overloaded function to calculate the square of a floating-point number
double square(double x) {
return x * x;
}
int main() {
std::cout << "Square of 5 (integer): " << square(5) << std::endl;
std::cout << "Square of 3.5 (floating-point): " << square(3.5) << std::endl;
return 0;
}
// output //
Square of 5 (integer): 25
Square of 3.5 (floating-point): 12.25
square
function to accept both integers and floating-point numbers.main
, we called the square
function with both integer and floating-point arguments.square
function correctly computes the square based on the argument type, producing the expected results.Function overloading is a powerful feature in C++ that allows you to define multiple functions with the same name but different parameter lists. By leveraging function overloading, you can write more concise and readable code, enhance code reusability, and provide flexibility in function usage. Understanding how to effectively use function overloading is an essential skill for C++ programmers.Happy coding! ❤️