Program to Demonstrate Function Overloading in C++

In the world of C++ programming, one of the fundamental concepts that enhance the flexibility and usability of code is function overloading. Function overloading allows the programmer to define multiple functions with the same name but different parameters. This feature is particularly useful in improving code readability and functionality. By understanding and implementing function overloading, programmers can write more intuitive and maintainable code.

Function overloading follows the principle of polymorphism in object-oriented programming, enabling a function to behave differently based on its input parameters. The compiler differentiates these functions based on the number or types of parameters, allowing the same function name to serve various purposes.

Prerequisites

Before diving into examples of function overloading, it’s essential to have a basic understanding of the following C++ concepts:

  • Functions and their syntax
  • Data types
  • Basic input and output operations
  • Compiling and running a C++ program

Understanding Function Overloading

Function overloading in C++ can be achieved by defining multiple functions with the same name but different parameters, either in terms of number or type. Here’s a simple formula to keep in mind:

Function Signature = Function Name + Parameter List

The compiler uses the function signature to differentiate between the overloaded functions.

Examples of Function Overloading

1. Overloading Functions by Varying Number of Parameters

Example 1.1: Summing Integers

C++
#include <iostream>
using namespace std;

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

int sum(int a, int b, int c) {
    return a + b + c;
}

int main() {
    cout << "Sum of 2 and 3: " << sum(2, 3) << endl;
    cout << "Sum of 1, 4, and 5: " << sum(1, 4, 5) << endl;
    return 0;
}

Output:

C++
Sum of 2 and 3: 5
Sum of 1, 4, and 5: 10

Explanation: In this example, the sum function is overloaded with two versions. The first version takes two integers, while the second version takes three integers. Depending on the number of arguments passed, the appropriate function is invoked.

2. Overloading Functions by Varying Type of Parameters

Example 2.1: Multiplying Numbers

C++
#include <iostream>
using namespace std;

int multiply(int a, int b) {
    return a * b;
}

double multiply(double a, double b) {
    return a * b;
}

int main() {
    cout << "Product of 2 and 3: " << multiply(2, 3) << endl;
    cout << "Product of 2.5 and 3.5: " << multiply(2.5, 3.5) << endl;
    return 0;
}

Output:

C++
Product of 2 and 3: 6
Product of 2.5 and 3.5: 8.75

Explanation: Here, the multiply function is overloaded based on the type of its parameters. One function handles integer multiplication, while the other handles double multiplication.

3. Overloading Functions with Mixed Parameter Types

Example 3.1: Displaying Values

C++
#include <iostream>
using namespace std;

void display(int a) {
    cout << "Integer: " << a << endl;
}

void display(double a) {
    cout << "Double: " << a << endl;
}

void display(char a) {
    cout << "Character: " << a << endl;
}

int main() {
    display(5);
    display(3.14);
    display('A');
    return 0;
}

Output:

C++
Integer: 5
Double: 3.14
Character: A

Explanation: In this case, the display function is overloaded to handle different data types: integer, double, and char. Depending on the type of the argument passed, the corresponding function is invoked to display the value.

Conclusion

Function overloading is a powerful feature in C++ that enhances the versatility and readability of code. By allowing multiple functions with the same name but different parameters, programmers can create more intuitive and maintainable programs.