搜尋此網誌

2025年9月9日星期二

A typical example of function overloading

#include <iostream>

using namespace std;


// Overloaded print function for int

void print(int i) {

    cout << "Printing int: " << i << endl;

}


// Overloaded print function for double

void print(double d) {

    cout << "Printing double: " << d << endl;

}


// Overloaded print function for string

void print(const string& s) {

    cout << "Printing string: " << s << endl;

}


int main() {

    print(42);           // Calls print(int)

    print(3.14);         // Calls print(double)

    print("Hello C++");  // Calls print(string)

    return 0;

}


Microsoft Copilot

沒有留言:

發佈留言