Here’s a C++ study note that covers some of the basics and important points of C++:
I. C++ Basics
1. Basic data types
- Integer:
- ‘int’: typically 32 bits, storing integers, e.g. 'int num = 10; `。
- ‘long’:一般为 64 位,存储较大范围的整数,如 ‘long bigNum = 123456789012345L;’。
- ‘short’: typically 16 bits, suitable for storing a smaller range of integers, 'short smallNum = 32767; `。
- Floating-point:
- ‘float’: single-precision floating-point number, low precision, stores numbers with decimals, 'float pi = 3.14f; `。
double:双精度浮点数,精度更高,是常用的浮点数类型,double moreAccuratePi = 3.141592653589793;。
- Character Type:
char:存储单个字符,如char letter = 'A';。- ‘string’:存储字符串,需要包含 ‘’ 头文件,‘string name = “John”;’。
2. Variables and constants
-Variable:
- When defining a variable, you need to specify the data type, and then you can assign and modify the variable, for example:
cpp int age; age = 25; age = age + 1;
- It can also be initialized directly at definition time: 'int score = 85; `。
-Constant:
- The ‘const’ keyword can define constants, and the value of the constant cannot be modified after initialization, such as:
cpp const int MAX_VALUE = 100;
- You can also use ‘#define’ to define constants, but it’s not recommended because it’s a preprocessor instruction and not type-safe:
cpp #define PI 3.14159
3. operator
- Arithmetic Operators:
- Include ‘+’ (add), ‘-’ (minus), ‘*’ (multiply), ‘/’ (divide), ‘%’ (remainder), etc., e.g.:
int result = 10 + 5; int remainder = 10 % 3; - Relational Operators:
- For example, ‘>’ (greater than), ‘<’ (less than), ‘==’ (equal to), ‘!=’ (not equal to), ‘>=’ (greater than or equal to), ‘<=’ (less than or equal to), which are often used for conditional judgment:
if (score > 80) { cout << "优秀" << endl; } - Logical Operators:
- ‘&&’ (logical and), ‘||’(logical or), ‘!’ (logical not), used to combine multiple conditions, such as:
if (score >= 60 && score < 80) { cout << "及格" << endl; }
Second, control flow
1. Conditional statements
- if-else 语句:
For example:if (condition) { Executed when the condition is true } else { Executed when the condition is false }if (age >= 18) { cout << "成年"

最低0.47元/天 解锁文章
603

被折叠的 条评论
为什么被折叠?



