example4_with_references.cpp

 1
 2
 3
 4
 5
 6
 7
 8
 9
10
11
12
13
14
15
16
17
18
19
20
21
#include <iostream>

void increment(int& i) {
    i++;
    std::cout << "Incremented value (int&): " << i << std::endl;
}

void increment(double& d) {
    d++;
    std::cout << "Incremented value (double&): " << d << std::endl;
}

int main() {
    int a = 5;
    double b = 3.5;

    increment(a);  // Calls increment(int&)
    increment(b);  // Calls increment(double&)

    return 0;
}
Back to function_overloading