example3_with_default_arguments.cpp

 1
 2
 3
 4
 5
 6
 7
 8
 9
10
11
12
13
14
15
16
17
18
#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;
}

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, double) with both d and e specified

    return 0;
}
Back to function_overloading