example2_different_number_parameters.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 display(int i) {
    std::cout << "Displaying int: " << i << std::endl;
}

void display(int i, double d) {
    std::cout << "Displaying int and double: " << i << ", " << d << std::endl;
}

void display(int i, const std::string& s) {
    std::cout << "Displaying int and string: " << i << ", " << s << std::endl;
}

int main() {
    display(10);                       // Calls display(int)
    display(20, 3.14);                 // Calls display(int, double)
    display(30, "Function Overloading");// Calls display(int, std::string)

    return 0;
}
Back to function_overloading