1. 第一个C++程序
#include<iostream>
using namespace std;
int main() {
cout << "Hello world" << endl;
system("pause");
return 0;
}
2. 注释
- 用于在代码中说明解释,方便阅读代码。
- 单行注释
- //文字;
- 或者选取一块代码ctrl+/;
- 多行注释
- /* 文字*/;
3. 变量
-
给一段指定的空间命名,如:
-
int a = 10;
-
-
语法:数据类型 变量名 = 初始值;
-
C++中必须给变量初始值
4. 常量
-
不可以改变的数据
-
两张方式:
- 宏常量
-
#define 常量名 常量值,如:
-
#define day 7
-
-
通常在文件头定义
- const修饰的变量
-
const 数据类型 常量名 = 常量值;如:
-
const int day = 7;
-
5. 关键字
- C++中预先保留的单词(标识符)
- 即在定义变量和常量时不得使用的表示符
- 如下:
asm | do | if | return | typedef |
---|---|---|---|---|
auto | double | inline | short | typeid |
bool | dynamic_cast | int | signed | typename |
break | else | long | sizeof | union |
case | enum | mutable | static | unsigned |
catch | explicit | namespace | static_cast | using |
char | export | new | struct | virtual |
class | extern | operator | switch | void |
const | false | private | template | volatile |
const_cast | float | protected | this | wchar_t |
continue | for | public | throw | while |
default | friend | register | true | |
delete | goto | reinterpret_cast | try |
6. 标识符命名规则
- 不能是关键字
- 只能由字母、数字、下划线组成
- 第一个字符必须为字母或下划线
- 大小写是不一样的
尽量见名知意,免于阅读。