Chapter 2 Notes on variables and Basic Types
2.1 Basic built-in types
-- C ++ defines a group of integers, floating-point numbers, single characters, and boolean values. It also defines a special type called void. The void type does not have a corresponding value. It is usually used as the return type of a function without a return value in a limited number of cases.
Type Description: minimum storage space
-Bool Boolean --
-Char character type 8-bit
-Wchar_t wide character 16-bit
-16-bit short integer
-Int integer 16-bit
-Long integer 32-bit
-Float Single-precision floating point 6-digit valid number
-Double floating point
-Long double extended precision floating point type 10-digit valid number
Summary of built-in types:
-The short, int, and long types indicate integer values, and the storage space size is different. Generally, the short type is a half-machine font, the int type is a machine font, and the long type is one or two machine font.
-The bool type indicates true and false values. Any value of the arithmetic type can be assigned to a bool object. The arithmetic type of the 0 value indicates false, and any non-0 value indicates true.
-What is the difference between the unsigned and signed types?
-The former is of the unsigned type and can only represent positive numbers greater than or equal to 0. The latter is a symbolic type that can represent positive numbers, negative numbers, and 0.
-The float type is generally represented by a single word (32-bit), the double type is represented by two words (64), and The longdouble type is represented by three or four words (96 or 128 words). Note: The float type can only ensure 6 Valid digits, while the double type can ensure at least 10 valid digits. Therefore, the double type is basically correct.
2.2-character nominal value constant (character constant)
Rules
-An integer constant of the nominal value starting with 0 indicates octal, and a constant starting with 0x or 0X indicates hexadecimal,
-The literal value integer constant is of the int or long type by default.
Floating-point nominal value rules
-The default floating point nominal value constant is double type. Add f to the value to indicate the single precision, and add L to indicate the extended precision.
-Generally, floating-point constants can be expressed in decimal or scientific notation.
Boolean and character nominal values
-Use true and false to indicate the Boolean nominal value: bool test = false
-The reserved value is defined by a pair of single quotes, such as: ('A ')
Escape Character constant
-They all start with the backslash, for example: (\ n)
String constant
-It is represented by zero or multiple characters enclosed in double quotes, such as: ("hello world ")
-Note: The Compiler automatically adds an empty character ('\ 0') to the end of all string constants in C ++ ')
2.3 Variables
-Important: Before using a variable in a program, you must first define the type of the variable.
-Left value (lvalue): it can appear on the left or right of the value assignment statement.
-Right Value (rvalue): it can only appear on the right of the value assignment and cannot appear on the left of the value assignment statement.
-The left value is the address of the variable, and the right value is the value of the variable.
-The variable is the left value, so it can appear on the left of the value assignment statement. The numeric constant is the right value, so it cannot be assigned a value.
-What is a variable: a bucket with a name is an object with a name.
-What is an object: an object is generated when the left expression is calculated for a region of type in the memory.
Variable name
-The identifier of a variable. It can consist of letters, numbers, and underscores. The variable name must start with a letter or underscore and be case-sensitive, such as somename.
-Note: The identifier cannot contain two consecutive underlines, nor must it start with followed by an uppercase letter.
-The identifier defined outside the function cannot start with an underscore
Define object
For example, intunits_sold;
-Start with the type name + identifier +;
-End definition with a semicolon
Initialization
-Two Types of initialization variables: Copy initialization and direct initialization.
For example: int ival (1024);-directly initialize
Int ival = 1024;-copy Initialization
-Note: initialization and assignment in C ++ are two different operations.
Description and definition
-The definition of a variable is used to allocate storage space for the variable. You can also specify the initial value for the variable. In a program, there is only one definition.
-Declaration is used to indicate the type and name of the variable to the program.
-Definition is also Declaration: when defining a variable, we declare its type and name.
-Declarations that do not define variables include the object name, object type, and the keyword extern before the object type:
-For example, extern int I; declared variable name I does not define it.
-The extern Declaration is not a definition and no space is allocated.
-Only when the Declaration is also defined, the Declaration can have an initialization type, because only the definition can allocate storage space.
-The initialization type can be included only when the extern declaration is outside the function.
Name Scope
-Local variables: internal function definitions and statement scopes
-Global variables: non-function variables
2.4 const qualifier
-Const is a keyword used to define constant variables, that is, to convert an object into a constant.
-Annotation: Because constants cannot be modified after being defined, they must be initialized during definition.
-By specifying the const variable as extern, you can access the const object throughout the program. Otherwise, the const variable can only be a local variable.
-Note: The default value of a non-const variable is extern. To make the const variable accessible in other files, it must be explicitly specified as extern.
2.5 reference
-Important Concept: Reference is just another name of an object.
-Compound type, which is defined "&".
-The reference must be used to initialize the object of the same type as the reference:
Int ivals = 1024;
Int & refval = ival;
Const reference
-Const reference refers to reference of const.
For example, const intival = 1024;
Const int & refval = ival; can read but cannot modify refval,
-Const reference can be initialized to different types of objects or initialized to the right value.
-Note: Non-const references can only be bound to objects of the same type as the reference.
Const reference can be bound to different but related types of objects or to the right value.
Typedef name
-Synonym used to define the type: typedef double wages; // wages is a synonym for double
-Typedef is usually used for the following three purposes:
To hide the implementation of a specific type, emphasize the purpose of using the type.
Simplify complex type definitions to make them easier to understand.
One type is allowed for multiple purposes, and the purpose of each use of this type is clear.
Enumeration (enum)
Define and initialize Enumeration
For example, enumopen_modes (input, output, append );
It includes the following parts:
The enum keyword is an optional Enumeration type name and a list of enumerated members separated by commas (,) enclosed in curly brackets.
-The value used to initialize enumeration members must be a constant expression.
Class type
-The class definition starts with the keyword class, followed by the name identifier of the class. The class is in curly brackets.
-Note: The curly braces must be followed by a semicolon.
For example:
Class Sales_item {
Public:
// Operations on Sales_item objects willgo here
Private:
Std: string isbn;
Unsigned units_sold;
Double revenne;
};
Resolution: public and private are called member access delimiters. They are used to declare the access attributes of each member.
The curly braces contain the class body, which defines the data and operations that constitute this type.
-Operations are called member functions, while data is called data members.
-Note: the only difference between struct and class is that they define classes at the default access level: by default, struct members are public, the class member is private. Another point is that struct has no constructor.
Compile your own header file
-The header file generally contains the class definition, extern variable declaration, and function declaration.
Correct use of header files brings two benefits:
Ensure that all files use the same declaration of the given object;
When the Declaration needs to be modified, only the header file needs to be updated;
-The header file is used for declaration instead of definition.
-The header file can define the const object and inline function that are known after compilation.
-Some const objects are defined in the header file so that the compiler can see their initialization format.
-If the const variable is not initialized using a constant expression, it should not be defined in the header file.
Introduction to pre-processors
-# Include indicates that only one parameter is accepted: header file name
-Important: when designing the header file, you should make it possible to include it in the same source file multiple times.
-The Preprocessor variable has two states: defined or undefined.
# Define indicates that a name is accepted and a pre-processor variable is defined.
# Ifndef indicates whether to check whether the specified Preprocessor variable is undefined. If not defined, all the instructions following it are processed Until # endif appears.
Note: If the header file name is enclosed in angle brackets (<>), the header file is considered as a standard header file.
If the header file name is enclosed in a pair of quotation marks, it is considered as a non-system header file.
Summary:
The Notes in this chapter are all here, because there is a certain foundation before, it is not difficult to learn this chapter, I found that some of the names and names of this book are different from those of Tan boo. For me, I prefer the name of Tan boo, which is easy to understand, I began to slowly find that some of this book's parsing is complicated, and new terms and content have been introduced. It is a challenge for me to grasp it all at once, in order to avoid forgetting the chapter too quickly, take notes within three days and extract important content.
From: The Path of wwj's dream