example3b_additional_overloads.cpp

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

void show(int i, int j = 0) {
    std::cout << "i = " << i << ", j = " << j << std::endl;
}

void show(double d, double e = 1.0) {
    std::cout << "d = " << d << ", e = " << e << std::endl;
}

void show(int i, double e) {
    std::cout << "i = " << i << ", e = " << e << std::endl;
}

void show(double d, int j) {
    std::cout << "d = " << d << ", j = " << j << std::endl;
}

int main() {
    show(5);              // Calls show(int, int) with j defaulting to 0
    show(5, 10);          // Calls show(int, int) with both i and j specified
    show(3.14);           // Calls show(double, double) with e defaulting to 1.0
    show(2.71, 2);        // Calls show(double, int) due to the exact match

    return 0;
}
Back to function_overloading