1 命名空间namespace
标准命名空间std;
命名空间使用方法:
方法1:
#include <iostream>
using namespace std;
cout << "hello world" << endl;
方法2:
#include <iostream>
using std::cout;
using std::endl;
cout << "hello world" << endl;
方法3:
#include <iostream>
std::cout << "hello world" << std::endl;
命名空间可以嵌套:
namespace space_1{
int id;
namespace space_2{
int id;
}
}
space_1::id = 10;
2 全局变量
全局变量不能重复定义
3 添加bool类型
bool flag = true;
flag = false;
bool类型的值
true: 1
false : 0
4 三目运算符增强
三目运算符可以当做左值
int a = 10;
int b = 100;
(a > b ? a : b) = 20;
5 const类型增强
const类型在c++编译器里,在编译阶段会转化为符号表里的键值对;
c++里的const是真正的常量;
const int num = 10;
int arr[num] = {0};
const int num = 10;
int* p = (int*)#
*p = 100;
cout << num << endl;
cout << *p << endl;
输出结果是num = 10,*p = 100,在这里C++编译器会临时对常量num取一个临时地址给p;
同时c++里的常量与#define宏定义的区别在于,宏定义是在预处理器阶段,常量是在编译阶段;
6 枚举的增强
enum season{
spr = 0,
sum ,
aut ,
win ,
};
season s = spr;
枚举的赋值只能是枚举定义里的值,不能是数字;