1.define
define 变量名 数值
2.sizeof
#include <iostream>
using namespace std;
int main(void)
{
cout << "int类型所占空间的大小是:" <<sizeof(int)<< endl;
system("pause");
return 0;
}
3.switch运用法
switch(表达式)
{
case 结果1:
执行语句;
break;
......
default:
执行语句;
break;
}
4.goto
goto sb;......
sb:......
5.二维数组
数据类型 数组名[行][列];
数据类型 数组名[行][列] = {{数据1,数据2},{数据3,数据4}};
数据类型 数组名[行][列] = {数据1,数据2,数据3,数据4};
数据类型 数组名[][列] = {数据1,数据2,数据3,数据4};
6.函数的返回值
#include<iostream>
using namespace std;
int& test1()
{
int a = 10;//栈区
return a;
}
int& test2()
{
static int b = 20;//静态变量存放在全局区,全局区的数据在程序结束后系统释放
return b;
}
int main(void)
{
int& ret = test1();
int& ret2 = test2();
cout << ret2 << endl;
//作为左值
test2() = 1000;//如果函数的返回值是引用,这个函数调用可以作为左值
cout << ret2 << endl;
cout << ret << endl;//第一次结果正确是因为编译器做了保留
cout << ret;//第二次结果错误是因为a的内存已经释放
return 0;
}
6.struct和class的区别
在C++中struct和class的唯一区别就是默认的访问权限不同。
区别:
struct默认权限为公共public
class默认权限为私有private
成员属性设置为私有
优点1:将所有成员属性设置为私有,可以自己控制读写权限。
优点2:对于写权限,我们可以检测数据的有效性。