-
common code
#include <iostream>
using namespace std;
int main() {
cout << "Hello, World!" << endl;
//字符串与变量一起输出时用<<分隔
cout <<"一周总共有:"<< Day << "天"<< endl;
//输入
int a =0;
cin >> a;
return 0;
}
-
constant
There are two ways to define constants in C++
1. #define macro constants: #define constant name constant star value.
Usually defined above the file to represent a constant. Pay attention to the format, there is no equal sign.
2. Variables modified by const: const data type constant name = constant value
Usually, the keyword const is added before the variable star definition to modify the variable as a constant and cannot be modified.
-
character
1. When displaying character variables, enclose the characters in single quotes instead of double quotes
2. There can only be one character in single quotes, not a string
3. Character variables in C and C++ only occupy 1 byte.
4. The character variable does not store the character itself in the memory, but puts the corresponding ASCII code into the storage unit
-
string
Two styles
1. C style string: char variable name []="string value" (note the square brackets)
2. C++ style string: string variable name = "string value" (note the introduction of the header file string)
-
Operation
- The result of dividing two integers is still an integer, and the decimal part is removed
- Modulo operation cannot be performed on two decimal numbers
- In C++, the ternary operator returns a variable , which can continue to be assigned
-
Procedure flow chart
- Disadvantages of switch: when judging, it can only be an integer or character type, not a range; advantages of switch: clear structure, high execution efficiency; if there is no break in the case, the program will continue to execute downward.
-
pointer
Occupied space size:
Under the 32-bit operating system, the pointer occupies 4 bytes of space, no matter what data type
Under the 64-bit operating system, the pointer occupies 8 bytes of space
null pointer:
A null pointer is used for pointer initialization and is not accessible
const modification:
1. const modifier constant, pointer constant. int * const p
The pointer cannot be changed, but the value pointed to can be changed.
2. const modified pointer, constant pointer. const int *p
The pointer can be changed, but the pointed value cannot be changed.
3. Modified at the same time, neither the pointer nor the value can be changed.
-
memory partition model
Code area: store the binary code of the function body, managed by the operating system
Global area: store global variables, static variables and constants
Stack area: automatically allocated and released by the compiler, store function parameter values, local variables, etc.
Heap area: by the program If the programmer does not release, it will be recovered by the operating system at the end of the program