1. 变量、赋值与表达式
变量(variable) 是具名对象,赋值(assignment) 用 = 运算符(= operator) 改变其值,初始化(initialize) 赋予初值,避免 未初始化的变量(uninitialized variable) 引发错误。表达式(expression) 是最小的计算单元,包含运算对象和运算符。
示例:
#include <iostream>int main() {int x = 5; // 初始化x = 10; // 赋值std::cout << "x + 2 = " << (x + 2) << std::endl; // 表达式return 0;
}
2. 块、语句与函数
程序块(block) 是用 花括号(curly brace) {}
包裹的语句序列。语句(statement) 是程序的基本执行单位,如表达式加分号、if 语句(if statement)、for 语句(for statement) 和 while 语句(while statement)。
函数(function) 是具名计算单元,包含 函数名(function name)、形参列表(parameter list)、返回类型(return type) 和 函数体(function body)。main 函数 是程序入口,使用 () 运算符(() operator) 调用其他函数。
示例:
#include <iostream>int square(int num) { // 函数定义return num * num;
}int main() {int x = 4;{ // 程序块int result = square(x); // 语句std::cout << "Square of " << x << " is " << result << std::endl;}return 0;
}
3. 流、缓冲区与操纵符
标准库(standard library) 的 iostream 头文件提供 ostream(输出流)和 istream(输入流)类型:
- cin:从 标准输入(standard input) 读取。
- cout:写到 标准输出(standard output)。
- cerr 和 clog:写到 标准错误(standard error),
cerr
无缓冲,clog
有缓冲。
<< 运算符(<< operator) 和 >> 运算符(>> operator) 处理输出和输入,数据暂存于 缓冲区(buffer)。操纵符(manipulator) 如 std::endl
换行并刷新缓冲区。
示例:
#include <iostream>int main() {int num;std::cout << "Enter a number: ";std::cin >> num;std::cout << "Result: " << num << std::endl;std::cerr << "Error test" << std::endl;return 0;
}
4. 条件与循环
条件(condition) 是真(非零)或假(零)的表达式,用于 if 语句、while 语句 和 for 语句。运算符包括:
- == 运算符(== operator):相等。
- != 运算符(!= operator):不相等。
- < 运算符(< operator)、> 运算符(> operator) 等。
示例:
#include <iostream>int main() {int num;std::cout << "Enter a number: ";std::cin >> num;if (num >= 0) { // >= 运算符std::cout << "Non-negative" << std::endl;} else {std::cout << "Negative" << std::endl;}while (num > 0) { // > 运算符std::cout << num << " ";--num; // -- 运算符}std::cout << std::endl;return 0;
}
5.内置类型与字符串
内置类型(built-in type) 如 int
由语言定义。字符串常量(string literal) 是双引号括起的字符序列(如 "hello"
)。类类型(class type) 由 类(class) 定义。
示例:
#include <iostream>int main() {int age = 25;std::string text = "Age: "; // 字符串常量赋值给类类型std::cout << text << age << std::endl;return 0;
}
6. 面向对象基础
类 定义 数据结构(data structure) 和操作,成员函数(member function) 或 方法(method) 用 点运算符(. operator) 调用。
示例:
#include <iostream>
#include <string>class Car {
public:std::string model;int speed;void accelerate() {speed += 10; // += 运算符std::cout << model << " speed: " << speed << std::endl;}
};int main() {Car myCar;myCar.model = "Tesla";myCar.speed = 50;myCar.accelerate();return 0;
}
7. 命名空间与头文件
命名空间(namespace) 如 std
用 :: 运算符(:: operator) 访问,避免名字冲突。头文件(header) 通过 #include
引入定义。
示例:
#include <iostream>int main() {std::cout << "Hello from std namespace" << std::endl; // :: 运算符return 0;
}
8. 开发与调试
程序存储在 源文件(source file) 中,开发是 编辑-编译-调试(edit-compile-debug) 过程。注释(comment)(//
或 /* */
)记录意图,文件结束符(end-of-file) 标记输入结束。
示例:
#include <iostream>int main() {int sum = 0, value;// 读取直到 EOF (Ctrl+Z 或 Ctrl+D)while (std::cin >> value) {sum += value;}std::cout << "Sum: " << sum << std::endl;return 0;
}
9.结语
从 变量 到 类,从 运算符 到 命名空间,这份术语表涵盖了 C++ 的核心。通过这些示例,你可以动手实践,体验 编辑-编译-调试 的乐趣。C++ 的世界很大,愿你在编程之旅中不断进步!
有什么问题?欢迎留言讨论!