本文共 660 字,大约阅读时间需要 2 分钟。
1、函数重载:
函数名相同、作用在同一作用域、参数可不相同。
例子:
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 26 27 28 29 30 31 32 33 34 35 | #include <iostream> #include<stdlib.h> using namespace std; int getMax( int i, int j){ int max=0; if (i>j) max=i; else max=j; return max; } int getMax( int arr[], int count) { int temp=arr[0]; for ( int i=1;i<count;i++) { if (temp<arr[i]) temp=arr[i]; } return temp; } int main( void ) { int arr1[5]={6,9,0,67,45}; cout<<getMax(1,5)<<endl; cout<< getMax(arr1,5)<<endl; system ( "pause" ); return 0; } |
运行结果:
鉴于函数名相同,函数在调用的过程中顺序为函数名--参数类型。来识别和区分。
本文转自 lillian_trip 51CTO博客,原文链接:http://blog.51cto.com/xiaoqiaoya/1961135,如需转载请自行联系原作者