C++ Program to Demonstrate Function Overloading

Function overloading in C++ allows you to define multiple functions with the same name but with different parameters. This feature enhances the readability and usability of your code by letting you use the same function name for different types or numbers of arguments. This article will guide you through the concept and implementation of function overloading in C++ with detailed explanations and examples.

Prerequisites

Before diving into the implementation, ensure you have a basic understanding of the following concepts:

  1. Basic C++ Syntax: Familiarity with C++ syntax, including variables, functions, and control structures (if-else, loops).
  2. Functions: Basic understanding of how functions work in C++.

1. Introduction to Function Overloading

1.1 Definition

Function overloading is a feature in C++ where two or more functions can have the same name but different parameters (number, types, or both). The correct function to be invoked is determined by the arguments passed during the function call.

1.2 Syntax

The general syntax for defining overloaded functions is as follows:

C++
void functionName(int arg1);
void functionName(double arg1);
void functionName(int arg1, int arg2);

2. Implementation of Function Overloading in C++

2.1 Example 1: Overloading Based on the Number of Parameters

Let’s start with a basic example to demonstrate function overloading based on the number of parameters.

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

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

void print(int a, int b) {
    cout << "Two integers: " << a << ", " << b << endl;
}

int main() {
    print(5);       // Calls the first version of print
    print(10, 20);  // Calls the second version of print

    return 0;
}

Output:

C++
Integer: 5
Two integers: 10, 20

Explanation:

  • The print function is overloaded with one and two integer parameters.
  • The correct version of print is called based on the number of arguments provided.

2.2 Example 2: Overloading Based on Parameter Types

This example demonstrates function overloading based on parameter types.

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

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

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

void print(string a) {
    cout << "String: " << a << endl;
}

int main() {
    print(5);           // Calls the int version of print
    print(5.5);         // Calls the double version of print
    print("Hello");     // Calls the string version of print

    return 0;
}

Output:

C++
Integer: 5
Double: 5.5
String: Hello

Explanation:

  • The print function is overloaded with int, double, and string parameters.
  • The correct version of print is called based on the type of argument provided.

2.3 Example 3: Overloading with Different Parameter Orders

This example shows how function overloading can be done with different parameter orders.

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

void display(int a, double b) {
    cout << "Integer: " << a << ", Double: " << b << endl;
}

void display(double a, int b) {
    cout << "Double: " << a << ", Integer: " << b << endl;
}

int main() {
    display(10, 5.5);  // Calls the first version of display
    display(5.5, 10);  // Calls the second version of display

    return 0;
}

Output:

C++
Integer: 10, Double: 5.5
Double: 5.5, Integer: 10

Explanation:

  • The display function is overloaded with different orders of int and double parameters.
  • The correct version of display is called based on the order and type of arguments provided.

2.4 Example 4: Overloading with Default Arguments

This example demonstrates how function overloading works with default arguments.

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

void computeArea(int side) {
    cout << "Area of square: " << side * side << endl;
}

void computeArea(int length, int breadth) {
    cout << "Area of rectangle: " << length * breadth << endl;
}

void computeArea(double radius) {
    cout << "Area of circle: " << 3.14159 * radius * radius << endl;
}

int main() {
    computeArea(5);              // Calls the int version for square
    computeArea(10, 20);         // Calls the version for rectangle
    computeArea(7.5);            // Calls the double version for circle

    return 0;
}

Output:

C++
Area of square: 25
Area of rectangle: 200
Area of circle: 176.714

Explanation:

  • The computeArea function is overloaded for calculating the area of a square, rectangle, and circle.
  • The correct version of computeArea is called based on the number and type of arguments provided.

Conclusion

Function overloading in C++ enhances the flexibility and readability of your code by allowing multiple functions with the same name but different parameters. This article provided a comprehensive guide to function overloading in C++, demonstrating its application through examples based on the number of parameters, parameter types, parameter orders, and use with default arguments.