1.函数高级
1.1函数的默认参数
规则:
1.默认参数后面,只能为默认参数;
2.在函数中和函数的声明中,两者只能存其一由默认参数;
1.2占位参数
在函数的形参定义中,先给出数据类型,不给出数据变量名称;
目前还用不到;
1.3函数重载
作用:函数名可以相同,提高复用性
函数重载需满足条件:
1.同一个作用域下
2.同一个函数名称
3.函数参数中,类型不同或个数不同或顺序不同
注意:
1.int& a与const int& a可重载;int a与const int a不可重载;
2.当使用函数重载时候,尽量不要使用默认参数;
#include <iostream>
using namespace std;
void func(int& a)
{
cout << "int &" << endl;
}
void func(const int& a)
{
cout << "const int &" << endl;
}
//void func(int a)
//{
// cout << "int &" << endl;
//}
void func2(int a, int b = 10)
{
cout << "func2(int a, int b = 10)" << endl;
}
void func2(int a)
{
cout << "func2(int a, int b = 10)" << endl;
}
int main()
{
int a = 10;
func(a);
func(20);
//func2(a);
func2(10, 20);
}