- 命名空间
- 缺省函数
- 函数重载
//命名空间:namespace //定义一个命名空间 //含义:规定作用域 #include缺省函数#include namespace N1{ int a=10; int b = -1; int add(){ return a + b; } } using N1::b; using namespace N1; //命名空间的使用 int main(){ //使用方法一 printf("a+b=%dn",N1::add()); //使用方法二:using N1::b; printf("b=%dn", b); //使用方法三:using namespace N1; printf("a=%dn", a); system("pause"); return 0; }
定义:声明或定义函数时为函数的参数指定一个默认值。在调用该函数时,如果没有指定实参则采用该默认值,否则使用指定的实参。
//C++的输入输出 #include//必须 using namespace std;//必须 int add1(int a=0,int b=20){//全缺省参数 return a + b; } int add2(int a,int b=10){//半缺省参数 //对于半缺省参数:在函数声明和函数定义同时出现时,如果缺省值不同,编译器则不能确认该用哪个默认值,并且,缺省值是从右往左依次赋值 return a + b; } int main(){ int a,b; cin >> a >> b;//标准输入 cout < 函数重载 函数名相同,参数列表不同。
#includeusing namespace std; //函数重载:函数名相同,参数列表不同 void show(){ cout << "哈哈哈啊哈哈" << endl; } void show(char* str){ cout << str << endl; } int main(){ show(); show("啦啦啦啦啦"); system("pause"); return 0; }



