example1_function_overloading.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
#include <iostream>

class Printer {
public:
    void print(int i) {
        std::cout << "Printing int: " << i << std::endl;
    }

    void print(double f) {
        std::cout << "Printing float: " << f << std::endl;
    }

    void print(const std::string& s) {
        std::cout << "Printing string: " << s << std::endl;
    }
};

int main() {
    Printer printer;
    printer.print(10);            // Calls print(int)
    printer.print(3.14);          // Calls print(double)
    printer.print("Hello World"); // Calls print(const std::string&)

    return 0;
}
Back to polymorphism