2019年5月18日 星期六

C++ Overloading Function


C++ Overloading Function

當有兩個同名的函式,但需要不同的參數時,compiler會自動使用符合的函式
int multiply(int, int);
double multiply(double, double);

int main(){
    cout << multiply(2, 3) << endl;
    cout << multiply(2.4, 3.6) << endl;
    return 0;
}

int multiply(int a, int b) {
    cout << "int * int : " << endl;
    return a * b;
}
double multiply(double a, double b) {
    cout << "double * double : " << endl;
    return a * b;
}
當輸入integer會使用第一個multiply function
當輸入double會使用第二個multiply function
tags: C++

沒有留言:

張貼留言

Last Position of Target

Last Position of Target Lintcode : 458 / Leetcode : 34 Level : Easy Problem Find the last position of a target number in a sorted ...