#include
using namespace std;
void Func(int a = 10, int b = 20, int c = 30)
{
cout << "a = " << a << endl;
cout << "b = " << b << endl;
cout << "c = " << c << endl;
}
int main()
{
Func();
Func(1);
Func(1, 2);
Func(1, 2, 3);
return 0;
}
void Func(int a, int b = 20, int c = 30)
{
// ...
}
PS:
// Func.h
void f(int a = 10);
// Func.cpp
void f(int a)
{
// ...
}
// 规定将缺省值写在函数声明中
函数重载:C++中允许出现同名函数,这些同名函数的参数不相同(类型,个数,类型顺序)。
#include
using namespace std;
// 1. 类型不同
int Add(int x, int y)
{
return x + y;
}
double Add(double x, double y)
{
return x + y;
}
// 2. 参数个数不同
void f()
{
cout << "NULL" << endl;
}
// 错误示例:
// void f(int a = 10)
// {
// cout << a << endl;
// }
// main函数中调用f()时,无法确定调用f() 还是 f(int a = 10)
void f(int a)
{
cout << a << endl;
}
// 3. 类型顺序不同
void f(int x, double y)
{
cout << x << endl << y << endl;
}
void f(double x, int y)
{
cout << x << endl << y << endl;
}
C语言链接函数地址时,利用函数名找。(C语言中不存在同名函数)
Linux下函数名修饰规则:_Z + 函数名字符个数 + 函数名 + 参数类型首字母
引用不是定义一个新的变量,而是给已有变量取一个别名,它和它引用的变量共用同一块内存空间。
#include
using namespace std;
int main()
{
int a = 10;
int& b = a;// b为a的引用
b--;
cout << a << endl;
return 0;
}
