文章目录
1 变量定义的位置可以随用随定义
对于变量定义的位置可以随用随定义
#include <iostream>
using namespace std;
int main(void){
int i = 10;
for(int i = 0; i < 10; i++){
cout << "i = " << i << endl;
}
return 0;
}
c语言早期版本不支持随用随定义
2 对全局变量的检测能力加强
int g_val;
// int g_val = 10; error: Redefinition of 'g_val'
int main(void){
return 0;
}
一个变量不管是声明还是定义,只能出现一次
3 使用struct时 不需要再使用struct关键字
struct teacher{
int age;
char name[64];
};
int main(void){
teacher t1;
//c语言中必须使用 struct teacher t1; 否则报错:Must use 'struct' tag to refer to type 'teacher'
return 0;
}
4 新增bool类型
#include <iostream>
using namespace std;
int main(void){
//1 bool类型的值为0或1
bool flag = true;
cout << " flag = " << flag << endl;
//2 bool类型占一个字节
cout << "sizeof(flag) = " << sizeof(flag) << endl;
//3 给bool类型赋值非0的值,他的值都为1
flag = 10;
cout << "flag = " << flag << endl;
}
打印结果
flag = 1
sizeof(flag) = 1
flag = 1
5 const的增强
5.1 不能通过指针间接修改常量值
#include <iostream>
int main(void){
//1 c++中的const,确实对a起了保护作用, 不能通过指针间接改变a的值
const int a = 10;
int *p = (int*)&a;
// c语言中可以通过指针的间接赋值改变const的值
*p = 20; //此时*p改变的是临时变量的值,而不是a的值
printf("a = %d\n", a);
return 0;
}
输出结果
a = 10
5.2 另外引申出const的一些基础知识
//1 常整型数
const int a;
//2 常整型数
int const b;
//3 c是一个指向常整数的指针(所指向的内存数据不能被修改,但是本身可以修改)
int const * c;
//4 d常指针(指针变量不能被修改,但是它所指向内存空间可以被修改)
int * const d;
//5 指向常整型的常指针(指针和它指向的内存空间,均不能被修改)
const int * const e;
示例:
#include <iostream>
int main(void){
//1 常整型数
const int a = 10;
//a = 5;//error:Cannot assign to variable 'a' with const-qualified type 'const int'
//2 常整型数
int const b = 20;
//b = 20;//error:Cannot assign to variable 'b' with const-qualified type 'const int'
//3 c是一个指向常整数的指针
int c_value = 30;
const int *c = &c_value;
printf("*c = %d\n",*c);
//本身可以修改
c = &b;
printf("*c = %d\n",*c);
//指向的内存数据不能修改
//*c = 40; error:Read-only variable is not assignable
//4 d常指针
int d_value = 50;
int * const d = &d_value;
*d = 60;
//指向的内存的值可以修改
printf("d_value = %d\n", d_value);
// 本身不可以修改
//d = &c_value; //error:Cannot assign to variable 'd' with const-qualified type 'int *const'
return 0;
}
输出结果
*c = 30
*c = 20
d_value = 60
5.3 c语言中的const是冒牌货
本来是想记录一下,C语言中的const是个冒牌货,但实际上C语言中也不并不能修改const修饰的变量的值。
理论上C语言中是能通过指针间接修改const修饰的变量的值的,但是我实际上测试是并不能
int main(void){
const int a = 10;
printf("a = %d\n", a);
int *p = &a;
*p = 20;
int array[a] ={10,20};
printf("a = %d\n", a);
printf("*p = %d\n", *p);
printf("array[1] = %d\n", array[1]);
return 0;
}
打印结果
a = 10
a = 10
*p = 20
array = 20
6 枚举加强
c语言中枚举的本质就是整型,枚举变量可以用任意整形赋值
c++ 中不能直接把int值赋给enum类型,只能用枚举元素初始化
#include <iostream>
using namespace std;
enum season{
SPR,
SUM,
AUT,
WIN
};
int main(void){
enum season s = SPR;
cout << "s = " << s << endl;
// s = 0; error:Assigning to 'enum season' from incompatible type 'int'
s = SUM;
cout << "s = " << s << endl;
return 0;
}
打印结果
s = 0
s = 1
7 命名空间
7.1 命名空间的使用
(1) 直接用std::cout 方式
#include <stdio.h>
#include <iostream>
int main(void){
std::cout << "hello world" << std::endl;
return 0;
}
输出结果
Hello world
(2) 使用using namesapce std 方式
using namespace std;
int main(void){
cout << "Hello world" << endl;
return 0;
}
输出结果
Hello world
(3) 使用std::cout
using std::cout;
int main(void){
cout << "Hello world" << std::endl;
return 0;
}
输出结果
Hello world
7.2 自定义命名空间
(1) 定义一个命名空间
namespace China {
int peopleSize = 14;
}
using namespace std;
int main(void){
using namespace China;
std::cout << " China::peopleSize = " << China::peopleSize<< std::endl;
return 0;
}
输出结果
China::peopleSize = 14
(2) 命名空间嵌套
namespace China {
int peopleSize = 14;
namespace HeiBei{
int peopleSize = 5;
}
}
using namespace std;
int main(void){
using namespace China;
std::cout << " China::HeiBei::peopleSize = " << China::HeiBei::peopleSize<< std::endl;
return 0;
}
输出结果
China::HeiBei::peopleSize = 5
8 C++中所有变量和函数都必须有类型
(1) c++中类型检测加强
#include <iostream>
// c++中函数不加返回值类型报错 error:Unknown type name 'f'
// c++中函数参数不加类型报错 error:Unknown type name 'i'
f(i){
printf("i = d% \n",i);
};
(2) 在c语言中 int f() 表示接收任意参数的函数
void f_2(){
printf("method f_2()\n");
};
int main(void){
f_2(10);
return 0;
}
输出结果
method f_2()
(3) c语言中,int f(void)表示无参函数
void f_3(void){
printf("method f_3(void)\n");
}
int main(void){
// f_3(10); error: Too many arguments to function call, expected 0, have 1
return 0;
}
c++中,int f() 和 int f(void)具有相同的意义,都表示返回值为int的无参函数
9 三目运算符功能增强
- C语言中三目运算符返回变量的值,所以不可以作为左直
- C++语言返回的是变量本身,所以可以作为左值
#include <iostream>
int main(void){
int a = 10;
int b = 20;
int min_value = 0;
(a < b ? a : b) = 30;
printf("a = %d,b = %d \n", a, b);
return 0;
}
输出结果
a = 30,b = 20
原理:C++编译器做了一个取地址的操作
*(a < b ? &a : &b) = 30;