[CONCLUSION:]
- Type is the basis of C ++ Program Design
- Each type defines its bucket requirements and operations that can be performed on all objects of this type. C ++ provides a set of basic built-in types, such as int and char. these types are closely related to their representation on machine hardware.
- Types can be divided into const and non-const. The const object must be initialized and its value cannot be modified. in addition, we can also define composite types, such as references. references provide another name for the object. composite types are classes defined by other types.
- The C ++ language supports customizing types by defining classes. The standard library uses class facilities to provide a set of advanced abstract concepts, such as IO and string types.
- C ++ is a static language: variables and functions must be declared before use. Variables can be declared multiple times but can only be defined once. It is always a good idea to initialize variables.
| Type |
Meaning |
Minimum storage space |
| Bool |
Boolean |
-- |
| Char |
Character Type |
8-digit |
| Wchar_t |
Wide character type |
16-bit |
| Short |
Short integer |
16-bit |
| Int |
Integer |
16-bit |
| Long |
Long Integer |
32-bit |
| Float |
Single precision floating point type |
6-digit valid number |
| Double |
Dual-precision floating point |
10-digit valid number |
| Long doble |
Extended precision floating point type |
10-digit valid number |
- The maximum (minimum) values of these types vary depending on the number of digits.
- The most basic way to make storage structured is to use chunk to process storage. Generally,.
- Signed and unsigned types
-
- The unsigned int type can be simplified to unsigned. That is to say, the unsigned int type is not added after the unsigned.
- If the sign bit is 1, the value is negative. If the sign bit is 0, the value is 0 or positive.
- For the unsigned type, the negative number always exceeds the value range. Unsigned objects may never save negative numbers. However, in C ++, it is completely legal to assign a negative value to an unsigned object. The result is the value after the negative value is calculated for the number of values of this type.
Because the complement code of-1 is 11111111 (the absolute value of the original code is reversed by adding one), the result is the binary code of 255.
- Float, float, double, and long double indicate Single-precision floating-point, double-precision floating-point, and extended-precision floating-point respectively.
-
- The float type is generally represented by a single word (32 bits), and the double type is represented by two words (64 bits, the long double type is represented by three or four characters (96-bit or 128-bit.
- In practice, float type precision is usually not enough-float can only ensure 6 Valid digits, while double type can ensure at least 10 valid digits, can meet the needs of most calculations.
- 42. Such values are treated. A literal value can only be called by its value. A constant indicates that its value cannot be modified. Only the built-in types have the literal volume, and there is no literal volume of the class type, so there is no literal volume of any standard library type.
-
- 20 // decimal
- 024 // octal
- 0x14 // hexadecimal
- The default type of the literal value integer constant is int or long. The precision type is determined by the literal value. The value is suitable for int type and long type. You can also add a suffix to forcibly convert the literal integer constant to the long, unsigned, or unsigned long type.
- Create a constant of the long type by adding L or l to the value (uppercase L is recommended, because lowercase l is easy to confuse with 1)
- Similarly, you can add U or u to the value to define the unsigned type. Adding L and U at the same time can obtain the nominal value constant of the unsigned long type (there is no space between the suffixes ).
Example: 128U/* unsigned */1024UL/* unsigned long */1L/* long */8Lu/* unsigned long */
-
- There is no literal constant of the short type.
- Floating Point nominal value rules:
-
- The default double type of a floating point literal value.
- Add F or f to the value to indicate the single precision, and add L or l to indicate the extended precision.
- Example: 3.14159F 12.345L 1.2345E1L
- Boolean and character nominal values:
-
- Printable character faces are usually defined using a pair of single quotes:
'A '2 '','''
-
- Add L to the front of the character nominal value to obtain the wide character nominal value of the wchar_t type.
-
| Line Break |
\ N |
Horizontal Tab |
\ T |
| Vertical Tab |
\ V |
Escape Character |
\ B |
| Carriage Return |
\ R |
Feed character |
\ F |
| Alarm (Bell) |
\ |
Backslash |
\\ |
| Question No. |
\? |
Single quotes |
\' |
| Double quotation marks |
\" |
|
|
-
- We can express any character as a common escape character in the following form:
-
- \ Ooo the ooo here represents three Octal numbers, and these three numbers represent the numeric values of the characters.
-
| \ 7 |
\ 12 |
\ 40 |
\ 0 |
\ 062 |
/115 |
| Ringtone |
Line Break |
Space Character |
NULL Character |
'2' |
'M' |
-
-
- You can also use hexadecimal escape characters to define characters: \ xddd consists of a backslash, an x, and one or more hexadecimal numbers.
- String Literal Value:
-
- A string literal value constant is expressed by zero or multiple characters enclosed in double quotation marks.
- C ++ is compatible with C. The Compiler automatically adds an empty character to the end of all character strings.
- There is also a wide string literal value, plus L in front. For example, L "a wide string literal ".
- The wide string literal is a string of constant width characters and ends with a wide and empty character.
- Using this feature, you can write long string literal values:
3. Variables
- Left(Lvalue, pronounced ell-vallue) the left value can appear on the left or right of the value assignment statement.
- Right Value(Rvalue, pronounced are-valle) the right value can only appear on the right of the value assignment.
-
- The left value of the variable, so it can appear on the left of the value assignment statement.
- The numeric literal value is the right value, so it cannot be assigned a value.
- Variable name
- Variable naming conventions:
- Variable names generally Use lowercase letters.
-
- Difference initialization and assignment: initialization refers to creating a variable and assigning it an initial value, while the Assignment refers to erasing the current value of the object and replacing it with a new value;
- You can use a counter and a character to initialize a string object. The created object contains the specified characters that have been repeated for multiple times. The number of repetitions is specified by the counter:
Std: string all_nines (10, '9'); // all_nines = "9999999999" the only way to initialize all_nines is to initialize it directly.
- Initialization rules
-
- The system will automatically initialize the variable based on its type and position.
- Initialization of built-in type variables:
-
- Whether a built-in type variable is automatically initialized depends on the location of the variable definition. Variables defined externally in the function body are initialized to 0.
- Built-in type variables defined in the function body are not automatically initialized.
- Class type variable initialization:
-
- Class controls class object initialization by defining one or more constructors.
- If the initialization type is not provided when defining a variable of a class, the default constructor is used. No matter where the variable is defined, the default constructor is used.
- Std: string empty; // empty is the empty string; empty = ""
- Description and definition
-
- Any variables used in multiple files must be declared separate from definitions. In this case, a file contains the definition of the variable, and other files that use the variable contain the declaration (rather than definition) of the variable ).
- Define a const object
-
- The const qualifier converts an object into a constant.
- Const int bufSize = 512; // input buffer size
- Define the bufSize as a constant and initialize it to 512. Any attempt to modify the bufSize will cause compilation failure;
- Since constants cannot be modified after definition, Initialization is required during definition.
- The const object is the local variable of the file by default.
-
- Unlike other variables, the declared const variable in the global scope is a local variable that defines the object. That is to say, this variable only exists in that file and cannot be accessed by other files.
- However, by setting the const variable to extern, you can access the const object throughout the program;
- The default value of a non-const variable is extern. To make the const variable accessible in other files, it must be explicitly set to extern.
5. The reference is another name of the object. The reference is mainly used as the form parameter of the function.
- A reference is a composite type. It is defined by adding the "&" symbol before the variable name,
- The reference must be initialized with an object of this type:
-
- Int iva1 = 1024;
- Int & refVa1 = ival; // OK, refVa1 refers to iva1
- Int & refVal12; // error, a reference must be initialized
- Int & refVa13 = 10; // error, initializer must be an object
- Initialization is the only method that specifies the object to which the reference is directed;
- A reference is just another name of an object;
- After the reference is initialized, as long as the reference exists, it is bound to the object to which it points during initialization and cannot bind the reference to another object.
- Const reference
-
- A const reference is a reference to a const object. It can be read but cannot be modified.
-
- Typedef can be used to define synonyms of types;
- Typedef is defined to start with the keyword typedef, followed by the Data Type and identifier.
-
- Typedef double wages; // wages is a synonymDouble
- Wages hourly = 12.45; // double hourly = 12.45
-
- We often need to define a set of optional values for some attributes.EnumerationIt not only provides a way to define an integer constant set, but also aggregates them into groups;
- Definition and initialization:
-
- Enum point {point1, point2, point3}; // point1 = 0; point2 = 1; point3 = 2;
- Enum point {point1 = 1, point2, point3}; // point1 = 1; point2 = 2; point3 = 3;
- Enum point {point1 = 1, point2, point3 = 5, point4, point5}; // point1 = 1; point2 = 2; point3 = 5; point4 = 6; point5 = 7;
- The enumerated member value cannot be changed. An enumerated member is a constant expression, so it can be used anywhere where a constant expression is needed.
-
- Each class defines an interface and an implementation.
- When defining a class, we usually first define the class interface, that is, the operation provided by the class. Through these operations, you can determine the data required for this class to complete its functions, and whether some functions need to be defined to support the implementation of this class.
- Define the Sales_item class
-
- Sales_item {
- Public:
- // Operations onSales_item objects will go here
- Private:
- }
-
- The last semicolon cannot be dropped!
- Data member of the class (orange part)
-
- When defining a data member, you can only specify the name and type of the data member.
- You Cannot initialize data members when defining data members in the class definition. Instead, you can control initialization through the special member functions of the constructor.
- Access ID
-
- Members defined in the public part can be accessed in any part of the program.
- The private part defines members, and the code that is not part of the class cannot be accessed.
- Use the struct keyword to define the class type. (Struct is inherited from C)
-
- If you use the class keyword to define a class, any member before the first access label is implicitly specified as private;
- If you use the struct keyword to define a class, all these members are public.
- The class or struct keyword is used to define the class, which only affects the default initial access level.
- The correct use of header files brings two benefits: ensure that all files use the same declaration of the given body; when the Declaration needs to be modified, only the header file needs to be updated;
- Header files are used for declaration rather than definition;
- Some const objects are defined in header files.
- Pre-processor
-
- # The include facility is part of the C ++ pre-processor. the source code of the pre-processor processing program runs before the compiler.
- # Include indicates that only one parameter is accepted: the header file name.
- To avoid name conflicts, Preprocessor variables are represented in uppercase letters.
-
- Preprocessor variables have two states: defined and undefined;
- # Define indicates accepting a name and defining the name as a Preprocessor variable; # ifndef indicates whether the pre-processor variable of the detection indicator is undefined. If the pre-processor variable is undefined, then all the instructions following are processed Until # endif appears;
- # Ifndef SALESITEM_H
- # Define SALESITEM_H
- // Use these facilities to prevent multiple accesses to the same header file
- # Endif
- # The include Directive accepts the following two forms:
-
- # Include <standard_header>
- # Include "my_file.h"
- If the header file name is included in the angle brackets, the header file is considered as a standard header file. The compiler will find this header file in the predefined location set.
- If the header file name is enclosed in a pair of quotation marks, it is considered as a non-system file, and the search usually begins with the path of the source file.