一、C++关键字
由于c++兼容c语言,c中的许多关键字也同样是c++的关键字,此外还有一些新加入的关键字,总览如下
二、命名空间
首先补充一个知识点,在使用函数、变量、结构体等等的时候,编译器默认查找顺序是局部域->全局域。
接下来看这样一段代码
#include<stdio.h>
#include<stdlib.h>
int rand = 0;
int main()
{
printf("%d ", rand);
}
在这段代码中,由于我们已经包含了rand函数的头文件,函数rand和整形rand同时在全局域中,由于他们的名字相同,在编译时会出错,无法确定选择哪一个。针对这种情况,c++之父就想到了一个好办法,给c++中加了一个技术叫做命名空间。
什么是命名空间?
用百度的回答就是,避免不同库或组件之间的命名冲突,并提供一种结构化的方式来组织和访问代码。其实就是允许不同的函数或者变量取相同的名字了。一般公司中的一个项目可能会由多个人完成,那么只要每个人都建立自己的命名空间,那么就不用怕命名冲突啦。
用法
namespace _name
{
int rand = 0;
int max(int a, int b)
{
return a > b ? a : b;
}
struct person
{
int age;
int height;
};
}
int main()
{
printf("%d ", _name:: rand);
}
这样子就不会出现编译错误啦,而且可以看到,在命名空间中可以定义变量,函数,类型等等
多个相同的命名空间
如果定义了多个相同的命名空间,那么编译器会认为他们在同一个命名空间中,
#include<iostream>
using namespace std;
namespace a
{
int x = 0;
}
namespace a
{
int y = 0;
}
int main()
{
cout << a::x << endl;
cout << a::y << endl;
}
所以说即使是设置了多个相同名字的命名空间,也不可以在这个命名空间中使用相同的命名函数变量等等。
作用域限定符
::是c++的作用域限定符,左边没有命名空间则默认访问全局
例:
#include<iostream>
using namespace std;
namespace zx
{
int rand = 20;
void DoubleAdd(int a, int b)
{
cout << 2 * (a + b) << endl;
}
}
int a = 21;
int main()
{
cout << ::a << endl;
cout << zx::rand << endl;
zx::DoubleAdd(1, 1);
return 0;
}
嵌套使用
编译空间也可以嵌套使用
#include<iostream>
using namespace std;
namespace bit
{
namespace a
{
void Add(int x, int y)
{
cout << x+ y <<endl;
}
}
}
namespace a
{
void Add(int x, int y)
{
cout << 2*(x + y) << endl;
}
}
int main()
{
bit::a::Add(1, 1);
a::Add(1, 1);
}
这样就可以在两个相同命名空间中设置一样名字的不同函数啦。
展开命名空间
命名空间的本质是定义一个命名空间域
,展开命名空间实际是增加编译器查找的顺序,除了在局部域和全局域找还在命名空间域找,它实际是一个声明,声明你可以访问这块空间也就是说,编译器的查找部分变为了
局部域->全局域->展开命名空间
#include<iostream>
using namespace std;
namespace zx
{
int c = 20;
void DoubleAdd(int a, int b)
{
cout << 2 * (a + b) << endl;
}
}
using namespace zx;
int main()
{
cout << c << endl;
DoubleAdd(1, 1);
return 0;
}
代码中的c和DoubleAdd就经历了局部域->全局域->展开命名空间的查找顺序
!需要注意的是,如果展开的多个命名空间中有同名的,也会引起冲突,如下:
#include<iostream>
using namespace std;
namespace zx
{
int c = 20;
}
namespace zx1
{
int c = 20;
}
using namespace zx;
using namespace zx1;
int main()
{
cout << c << endl;
return 0;
}
指定展开命名空间中的某一个
#include<iostream>
using namespace std;
namespace zx
{
int a = 0;
}
using zx::a;
int main()
{
cout << a << endl;
}
这样可以实现只把某个变量或函数展开到命名空间域中。
三、c++的输入与输出流
std
是c++标准库的命名空间名,c++将标准库
的定义实现都放到这个命名空间中。相比于c语言的printf和scanf,cout和cin可以自动识别变量的类型。但如果需要控制输入输出的精度,则使用printf和scanf更方便。
有以下几点注意
- <<是流插入运算符,>>是流提取运算符
- cin 和 cout 可以自动识别类型
- cout和cin是全局的流对象,endl是特殊的C++符号,表示换行输出,他们都包含在包含< iostream >头文件中,又在其中的命名空间std中。
cout和cin的使用
#include<iostream>
using std::cout;
using std::endl;
int main()
{
int a;
cout << 2 << endl;
std ::cin>> a;
}
由于cout使用相对于cin更频繁,一般会单独把cout拿到命名空间域。
四、缺省参数
缺省参数是声明或定义时为函数的参数指定的一个缺省值,或者说官方指导值。
如果用户在调用函数的时候没有传入对应的参数,则函数会自动使用已经设置好的默认值。
#include<iostream>
using namespace std;
void Func(int a = 0)
{
cout << a << endl;
}
int main()
{
Func();//未传入参数
Func(1);//传入参数1
return 0;
}
最后的输出为
0
1
缺省参数的分类
全缺省参数
即给每个形参一个默认值
void Func(int a = 0,int b=1,int c=2)
{
cout << a << endl;
cout << b << endl;
cout << c << endl;
}
int main()
{
Func();//1
Func(4);//2
Func(4,5);//3
Func(4,5,6);//4
}
输出:第1次 第2次 第3次 第4次
0 4 4 4
1 1 5 5
2 2 2 6
需要注意的是,输入的时候缺省值只能从右往左缺省。
半缺省参数
void Func(int a,int b=1,int c=2)
{
cout << a << endl;
cout << b << endl;
cout << c << endl;
}
int main()
{
Func(1);
Func(1,2,3);
}
给定一部分缺省参数,定义的时候不必遵循从右往左缺省的规则
注意:
在函数声明和函数定义处的缺省参数需要一致,一般仅在函数声明处定义缺省值;