作用:函数名可以相同,提高重复性。
函数重载满足条件:
- 同一个作用域下
- 函数名称相同
- 函数参数类型不同或者个数不同或者顺序不同
如:
#include<iostream>
using namespace std;
void func()
{
cout << "func的调用!" << endl;
}
void func(int a)
{
cout << "func(int a)的调用!" << endl;
}
void func(double a)
{
cout << "func(double a)的调用!" << endl;
}
void func(int a,double b)
{
cout << "func(int a,double b)的调用!" << endl;
}
void func(double b,int a)
{
cout << "func(double b,int a)的调用!" << endl;
}
int main()
{
func();
func(5);
func(3.14);
func(5, 3.14);
func(3.14, 5);
system("pause");
return 0;
}
注意:函数返回值不可以作为函数重载的条件。
函数重载的注意事项
- 引用作为重载条件
- 函数重载碰到函数默认参数