C++ C++ Tutorial C++ Functions C++ Classes



CPP Function Overloading

C++ is a powerful programming language that allows developers to create complex applications with ease. One of the key features of C++ is function overloading, which allows developers to create multiple functions with the same name but different parameters. This feature is incredibly useful for creating more readable and maintainable code, as well as improving the overall performance of an application.

Function overloading is a technique in C++ that allows developers to create multiple functions with the same name but different parameters. This means that a single function name can be used to perform different tasks depending on the type and number of arguments passed to it. This is achieved by defining multiple functions with the same name but different parameter lists.

For example, consider the following code:


int add(int a, int b) {
    return a + b;
}

double add(double a, double b) {
    return a + b;
}

In this example, we have defined two functions with the same name "add", but with different parameter lists. The first function takes two integers as arguments and returns their sum, while the second function takes two doubles as arguments and returns their sum. When we call the "add" function with two integers, the first function will be called, and when we call it with two doubles, the second function will be called.

Function overloading can also be used with default arguments. For example:


int add(int a, int b, int c = 0) {
    return a + b + c;
}

double add(double a, double b, double c = 0.0) {
    return a + b + c;
}

In this example, we have defined two functions with the same name "add", but with different parameter lists and default arguments. The first function takes two integers as arguments and an optional third integer argument with a default value of 0, while the second function takes two doubles as arguments and an optional third double argument with a default value of 0.0. When we call the "add" function with two integers, the first function will be called, and when we call it with two doubles, the second function will be called. If we call the "add" function with three arguments, the appropriate function will be called based on the types of the arguments.

Function overloading is a powerful feature of C++ that allows developers to create more readable and maintainable code. By defining multiple functions with the same name but different parameter lists, developers can create functions that perform different tasks depending on the types and number of arguments passed to them. This can greatly improve the performance of an application and make it easier to understand and modify.

Code Examples

Here are some examples of function overloading in C++:


#include <iostream>

using namespace std;

int add(int a, int b) {
    return a + b;
}

double add(double a, double b) {
    return a + b;
}

int main() {
    int x = add(1, 2);
    double y = add(1.5, 2.5);

    cout << "x = " << x << endl;
    cout << "y = " << y << endl;

    return 0;
}

In this example, we have defined two functions "add" with different parameter lists. We then call these functions with different arguments and store the results in variables "x" and "y". Finally, we print the values of these variables to the console.


#include <iostream>

using namespace std;

int add(int a, int b, int c = 0) {
    return a + b + c;
}

double add(double a, double b, double c = 0.0) {
    return a + b + c;
}

int main() {
    int x = add(1, 2);
    double y = add(1.5, 2.5);
    int z = add(1, 2, 3);
    double w = add(1.5, 2.5, 3.5);

    cout << "x = " << x << endl;
    cout << "y = " << y << endl;
    cout << "z = " << z << endl;
    cout << "w = " << w << endl;

    return 0;
}

In this example, we have defined two functions "add" with different parameter lists and default arguments. We then call these functions with different arguments and store the results in variables "x", "y", "z", and "w". Finally, we print the values of these variables to the console.

Reference

Activity