Function Overloading in C++

Function overloading is a feature that allows multiple functions to have the same name with different parameters. The compiler determines which function to invoke based on the number and type of arguments passed.

Key Characteristics

  1. Same Function Name: Multiple functions share the same name.
  2. Different Parameters: Differ in number or type of parameters.
  3. Compile-Time Resolution: The appropriate function is determined during compilation.

Example

#include <iostream>
using namespace std;

class Printer {
public:
    void print(int i) {
        cout << "Integer: " << i << endl;
    }
    void print(double d) {
        cout << "Double: " << d << endl;
    }
    void print(string s) {
        cout << "String: " << s << endl;
    }
};

int main() {
    Printer printer;
    printer.print(10);         // Calls print(int)
    printer.print(3.14);       // Calls print(double)
    printer.print("Hello");    // Calls print(string)
    return 0;
}