FUNCTION OVERLOADING:


In our previous tutorial on Operator Overloading, we discussed the concept of overloading and explored operator overloading. In this tutorial, we'll delve into function overloading.

When multiple functions employ the same name but with different arguments, they are termed as function overloading. It's a key feature of C++ that enables using the same function name for various functions to execute either similar or different tasks within the same class. Function overloading primarily aims to improve program readability. When we need to execute a single operation but with different types or numbers of arguments, function overloading comes into play.

Any function can be overloaded by altering the number or types of its arguments. Consider the following example:

// Function without parameters
void show() {
    // Function body
}

// Overloaded function using a single parameter
void show(int a) {
    // Function body
}

// Overloaded function using two parameters
void show(int a, char c) {
    // Function body
}

In the above example, we have three functions with the same name but different arguments. This means we've overloaded the function twice: once with a single argument and once with two arguments. This is how we can overload a function to perform different tasks. Let's consider a simple example where we'll overload a function to perform various tasks.

FUNCTION OVERLOADING - C++ Copy to Clipboard  
#include<iostream>
using namespace std;

class Overload {
public:
    // Function without parameters
    void show() {
        cout << "I am a function having no parameters." << endl;
    }

    // Overloaded function using a single parameter
    void show(int a) {
        cout << "I am an overloaded function having an integer: " << a << endl;
    }

    // Overloaded function using two parameters
    void show(int a, char c) {
        cout << "I am an overloaded function having two parameters: Int = " << a << " & char = " << c << endl;
    }
};

int main() {
    Overload load;
    load.show();
    load.show(15);
    load.show(15, 'c');

    return 0;
}