重载函数是函数的一种特殊情况,为方便使用,C++允许在同一范围中声明几个功能类似的同名函数,但是这些同名函数的形式参数(指参数的个数、类型或者顺序)必须不同,也就是说用同一个运算符完成不同的运算功能
1、重载条件:参数类型不同,参数个数不同,参数顺序不同,返回值不限制(返回值在计算到最后才会知道是什么类型的,但是在计算到最后之前必须知道调用的是哪个函数)
2、函数默认参数
#include <iostream>
#include <stdlib.h>
using namespace std;
void go(double c, int a=1, int b=2)
{
cout << a << b << c <<endl;
}
int main()
{
go(100.0);
return 0;
}
输出12100
#include <iostream>
#include <stdlib.h>
using namespace std;
void go(int a=1, int b=2,double c)
{
cout << a << b << c <<endl;
}
int main()
{
go(100.0);
return 0;
}
输出错误, //默认参数必须放在右边
//默认参数中间不允许出现不默认的
用指向函数的指针表示
#include <iostream>
#include <stdlib.h>
using namespace std;
void go(double c, int a=1, int b=2)
{
cout << a << b << c <<endl;
}
int main()
{
void (*p)( double c,int a, int b) = go;
p( 100.0,1, 2);
return 0;
}