C++ Function Overloading

Introduction

Function overloading in C++ allows you to define multiple functions with the same name but different parameter lists. This enables functions to handle different types or numbers of inputs while providing a consistent interface. Function overloading is a form of polymorphism, specifically compile-time polymorphism.

Defining Overloaded Functions

To overload functions, you create multiple functions with the same name but different parameter types, numbers, or both. The compiler distinguishes these functions based on their signatures, which include the function name and the parameter types.

Example: Basic Function Overloading

#include <iostream>
using namespace std;

// Function to add two integers
int add(int a, int b) {
    return a + b;
}

// Function to add two double values
double add(double a, double b) {
    return a + b;
}

// Function to add three integers
int add(int a, int b, int c) {
    return a + b + c;
}

int main() {
    // Call the overloaded functions
    cout << "add(3, 4): " << add(3, 4) << endl;           // Calls add(int, int)
    cout << "add(3.5, 4.5): " << add(3.5, 4.5) << endl;   // Calls add(double, double)
    cout << "add(1, 2, 3): " << add(1, 2, 3) << endl;     // Calls add(int, int, int)

    return 0;
}

Output

add(3, 4): 7
add(3.5, 4.5): 8
add(1, 2, 3): 6

Explanation

  • The add function is overloaded with three different signatures:
    • int add(int a, int b)
    • double add(double a, double b)
    • int add(int a, int b, int c)
  • The main function calls each version of the add function with appropriate arguments, and the correct function is invoked based on the argument types and number.

Rules for Function Overloading

  1. Different Parameter Types: Functions can be overloaded by changing the types of parameters.
  2. Different Number of Parameters: Functions can be overloaded by changing the number of parameters.
  3. Parameter Order: Functions can be overloaded by changing the order of parameters if their types differ.
  4. Return Type: The return type alone cannot be used to distinguish overloaded functions.

Example: Overloading by Parameter Order

#include <iostream>
using namespace std;

// Function to print an integer and a double
void print(int a, double b) {
    cout << "Integer: " << a << ", Double: " << b << endl;
}

// Function to print a double and an integer
void print(double a, int b) {
    cout << "Double: " << a << ", Integer: " << b << endl;
}

int main() {
    // Call the overloaded functions
    print(5, 3.14);   // Calls print(int, double)
    print(3.14, 5);   // Calls print(double, int)

    return 0;
}

Output

Integer: 5, Double: 3.14
Double: 3.14, Integer: 5

Explanation

  • The print function is overloaded with two different signatures:
    • void print(int a, double b)
    • void print(double a, int b)
  • The main function calls each version of the print function with appropriate arguments, and the correct function is invoked based on the parameter order.

Practical Examples

Example 1: Overloading Functions for Area Calculation

#include <iostream>
using namespace std;

// Function to calculate area of a rectangle
double area(double length, double width) {
    return length * width;
}

// Function to calculate area of a circle
double area(double radius) {
    return 3.14159 * radius * radius;
}

// Function to calculate area of a triangle
double area(double base, double height, bool isTriangle) {
    return 0.5 * base * height;
}

int main() {
    // Call the overloaded functions
    cout << "Area of rectangle: " << area(5.0, 3.0) << endl;         // Calls area(double, double)
    cout << "Area of circle: " << area(4.0) << endl;                 // Calls area(double)
    cout << "Area of triangle: " << area(4.0, 6.0, true) << endl;    // Calls area(double, double, bool)

    return 0;
}

Output

Area of rectangle: 15
Area of circle: 50.2654
Area of triangle: 12

Explanation

  • The area function is overloaded to calculate the area of different shapes:
    • Rectangle: double area(double length, double width)
    • Circle: double area(double radius)
    • Triangle: double area(double base, double height, bool isTriangle)
  • The main function calls each version of the area function with appropriate arguments, and the correct function is invoked based on the argument types and number.

Example 2: Overloading Functions for Printing Different Types

#include <iostream>
using namespace std;

// Function to print an integer
void print(int a) {
    cout << "Integer: " << a << endl;
}

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

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

int main() {
    // Call the overloaded functions
    print(5);          // Calls print(int)
    print(3.14);       // Calls print(double)
    print("Hello");    // Calls print(string)

    return 0;
}

Output

Integer: 5
Double: 3.14
String: Hello

Explanation

  • The print function is overloaded to print different types of data:
    • Integer: void print(int a)
    • Double: void print(double a)
    • String: void print(string a)
  • The main function calls each version of the print function with appropriate arguments, and the correct function is invoked based on the argument types.

Conclusion

Function overloading in C++ allows you to define multiple functions with the same name but different parameter lists. This feature enhances the flexibility and readability of your code by enabling functions to handle different types and numbers of inputs while providing a consistent interface. Understanding and effectively using function overloading is essential for writing robust and maintainable C++ programs.

Leave a Comment

Your email address will not be published. Required fields are marked *

Scroll to Top