The notes are based on C++Primer(5th Edition) and use C++11 standard
Because the author is a beginner, the content will not be comprehensive
If there is any mistake, please point it out directly
-2021.7
According to the content of C++ Primer, the notes will be divided into four parts:
- C + + Basics
- C + + standard library
- Class design
- Other C + + tools
Important Prerequisites
Write a program:
- Define variables
- Input Output
- Save and read data using data structure
- Processing data content
Now, let's get to know him from a simple C + + program
Each C + + program will contain 1 + functions, which stipulates that the main function is the main function that the operating system calls to run the program first. Here is an example of the simplest (nothing to do) main function.
int main(){ return 0; }
Here, let's explain:
Statements in C + + use semicolons as closing marks;
"/ /" means that the rest of the line is comments, which will not affect the compilation and operation of the program;
The function type needs to be declared before the function name. The example here is "int", so the return value after "return" must be of type "int" (subsequent talks and data type);
The return value "0" indicates that the program runs successfully. A non-zero value will be specifically defined to indicate different error types.
Compiling and running C++
The compiling and running process of the program is related to OS and compiler. You can learn compiling principles for details
First, we need to talk about putting the source code in the source file. Generally, the suffixes of the source file are:. cc,. cxx,. cpp,. cp,. c
Take GCC compiler as an example: execute commands on the console to compile program source files
gcc
variable
Built in type
Integer, character, Boolean, and floating point
type | meaning | Minimum size |
---|---|---|
bool | Boolean type | — |
char | character | 8 bits |
wchar_t | Wide character | 16 bit |
char16_t | Unicode character | 16 bit |
char32_t | Unicode character | 32 bit |
short | Short | 16 bit |
int | integer | 16 bit |
long | Long integer | 32 bit |
long long | Long integer | 64 bit |
float | Single-precision floating-point | 6 significant digits |
double | Double precision floating point number | 10 significant digits |
long double | Extended precision floating point number | 10 significant digits |
⭐ The basic character type char unit space shall ensure that the numerical value corresponding to any character in the basic character set of the machine is stored: the unit size shall be equal to the byte size of the machine
⭐ Regulation: an int ≥ a short; A long ≥ an int; One long ≥ one long;
In addition to Boolean and extended character types, integer types can be divided into signed and unsigned
literal constant
Int & Float:
20 // decimal/integer 024 // octal 0x3b // Hexadecimal 20u // unsigned integer 2000l // long integer 20ul // unsigned long integer 3.14159 // float 0.3141e1 // float/scientific notation u8"H" // utf-8 char u"H" // unicode char16_t U"hello" // unicode char32_t
Characters and Strings:
'a' // Character "abcdefg" // string
Escape Character:
"\\" // \ character "\'" // 'character "\"" // "character "\?" // ? Character "\a" // ringtone "\b" // backspace "\f" // change page "\n" // Newline "\r" // Enter "\t" // horizontal tab "\v" // vertical tab "\564" // An octal number from one to three digits "\x4f" // Hexadecimal number of one or more digits
variable
variable, which provides a named storage space for program operation. For C + +, variables and objects can be used interchangeably.
<type> <variable_list>; //Declare and define variables extern <type> <variable_list>; //Declare non variables
Naming conventions:
- Reflect the actual meaning
- Variable name lowercase
- Class name starts with capital e.g. College_student
- Identifiers composed of multiple words need to be distinguished (underline or hump method)
Composite type
e.g. reference, pointer
- Use & to reference and bind two values; Declaration reference must be initialized.
int ival = 1024; int &refVal = ival;
⭐ A reference is an alias
- The value of the pointer is usually an address (or null)
#include<iostream> using namespace std; int main(void){ int ival = 1024; int *refVal = &ival; cout << refVal << endl << *refVal; return 0; }
OUTPUT:
0x61fe14 1024
emmm ~ ~ ~ OK, let's tidy it up
int i = 1024; //Definition i int &r = i; //Indicates that r is a reference int *p; //Indicates that p is a pointer p = &i; //Fetch address character *p = i; //Dereference character int &r2 = *p; //Table application; Dereference character int **ppi = &p; //Pointer to pointer int *&ref = p; //Pointer reference // In the face of complex statements about references and pointers, you can read them from right to left to understand their meaning
const qualifier
When defining variables, the prefix const is added to limit the value of a variable to be unchangeable, which is convenient to adjust the size of a value in the whole domain of the program and prevent the value from changing during program operation.
⭐ References to constants:
const int i = 1024; const int &r = i; //√ int &r = i; //×
In addition:
int i = 1024; int &r1 = i; //The value of i can be modified by changing r1 const int &r2 = i; //The value of i cannot be modified by changing r2
Similarly:
A pointer to const pointing to a constant cannot be used to change the value of the constant;
A constant pointer cannot change the pointing object;
A const pointer to const pointing to a constant cannot change the pointing object or the value pointing to a constant;
int val = 1024; //variable const int cval = 1000; //constant const int *ptrc = &cval; //pointer to const int *const cptr = &val; //const pointer const int *const cptrc = &cval; //Constant pointer to constant *cval = 10; //You cannot change the value pointing to a constant ptrc = &val; //Can change the pointing constant *ptrc = 10; //You cannot change the value pointing to a constant cptr = &cval; //Cannot change the constant pointed to *cptr = 10; //Can you change the value pointing to a constant cptrc = &val; //Cannot change the constant pointed to *cptrc = 10; //You cannot change the value pointing to a constant
To sum up - in fact:
In the face of complex sentences, you can read them from right to left to understand their meaning
It can be seen that the position of const represents whether it points to a constant or a constant reference
Processing type
- Type alias
typedef <typename> <custom_ typename>; Indicates that the default data type is represented by a user-defined name, which is convenient to understand the purpose of the type, and the definition method is always passed, that is, typedef < custom_ typename1> <custom_ typename2>
C++ 11 has a new way to define aliases:
using <custom_typename> = <typename>;
Custom data structure
String, vector, array
Namespace using declaration
Standard library type
string
vector
iterator
array
expression
An expression consists of an operand, which is evaluated to a result. Literals and variables are the simplest expressions.
The operator acting on an operand is an unary operator;
The operator acting on two operands is binary operator;
The operator acting on the three operands is a ternary operator (only one);
Function calls are also special operators, and there is no limit to the number of objects.
- In complex expressions, operators conform to a certain priority, associativity, and order of evaluation
- The expression evaluation process will be accompanied by the conversion of data types. Generally, the small integer type will be promoted to the large integer type (mainly int); Pointer cannot be converted to a floating point number
Unfinished (2021.9)