函數多態也稱爲函數重載。ios
(1)函數重載指的是能夠有多個同名的函數,所以對名稱進行了重載。c++
(2)函數重載的關鍵在於函數的參數列表,也稱爲函數特徵標。若是兩個函數的參數數目和參數類型相同,同時參數的排列順序也相同,則他們的特徵標也相同,而變量名是可有可無的。git
(3)c++容許定義名稱相同的函數,條件是他們的特徵標相同。若是兩個函數的參數數目/或參數類型不一樣,則特徵標也不一樣。編輯器
(4)c++判斷是否重載的標準的函數的特徵標。函數
(5)若是僅僅是函數的返回值類型不一樣,而函數的特徵標相同,則不能認爲是重載。(有時咱們不用返回值,這樣編輯器就看不出來到底調用的是哪個函數)spa
Demo_01:3d
#include <iostream> unsigned long left (unsigned long num, unsigned int ct); char * left (const char *str, int n = 1); int main() { using namespace std; char *trip = "HelloWorld"; unsigned long n = 12345; char *temp; for (int i = 1; i < 11; i++) { cout << left(n, i) << endl; temp = left(trip, i); cout <<temp<<endl; delete [] temp; } return 0; } unsigned long left(unsigned long num, unsigned int ct) { unsigned digits = 1; unsigned long n = num; if (ct == 0 || num == 0) { return 0; } while (n /=10) { digits++; } if (digits > ct) { ct = digits - ct ; while (ct--) { num /= 10; } return num; } else { return num; } } char * left(const char *str, int n /*= 1*/) { if (n < 0) { n = 0; } char *p = new char[n+1]; int i, count; for (i = 0; i < n && str[i]; i++) { p[i] = str[i]; } while (i <= n) { p[i++] = '\0'; } return p; }
運行結果:blog