函數重載是C++中的一項函數,其中兩個或多個函數可以具有相同的名稱,但可以具有不同的參數。
函數重載可以視為C++中多態函數的一個示例。
以下是一個簡單的C++示例,以演示函數重載。
#include <iostream> 
using namespace std; 
  
void print(int i) { 
  cout << " Here is int " << i << endl; 
} 
void print(double  f) { 
  cout << " Here is float " << f << endl; 
} 
void print(char const *c) { 
  cout << " Here is char* " << c << endl; 
} 
  
int main() { 
  print(10); 
  print(10.10); 
  print("ten"); 
  return 0; 
}輸出:
Here is int 10 Here is float 10.1 Here is char* ten
注:本文由純淨天空篩選整理自 Function Overloading in C++。非經特殊聲明,原始代碼版權歸原作者所有,本譯文未經允許或授權,請勿轉載或複製。
