C ++ Primer: Chapter 2 summarizes

Overall structure

Chapter 2 basic types of variables and

2.1 The basic built-in types

Fundamental contents:

  1. Arithmetic type contains character, integer, floating point and boolean values, specific values ​​does not correspond to the type of space, only for the return type of the function return value is no special occasions.
  2. Type conversion: conversion from an object of a given type to another type.
  3. Literals: integer literals, floating point literals, character literals, string literals, escape sequences, prefix and suffix literals, Boolean literals, literal pointer.

Remarks:

  1. Integer including Boolean type and character types.
  2. bit is the smallest unit of data storage, byte is the basic unit of data processing, natural word integer arithmetic units: 1 word = 4 byte (32-bit machines), 1byte = 8 bit.
  3. Select Type: If the value is not negative selection unsigned type; int integer arithmetic selection; arithmetic expression or no char BOOL; floating-point operations selected double.
  4. Do not mix signed and unsigned types.

2.2 Variable

Fundamental contents:

  1. Variable definitions: type specifier + variable name list. Initialization, initialization list, default initialization.
  2. Variable definitions and variable declarations: declarations and definitions specify the type and name of the variable, in addition to the definition of application memory space, it may be initialized; variables can only be defined once, a statement many times.
  3. Identifier: letters, digits, and underscore, must start with a letter or underscore, case sensitive.
  4. Name Scope: global scope, block scopes, scopes inner, outer scope

Remarks:

  1. Initialization! = Assignment.
  2. A list initialized with {}. Default initialization, the function variable is initialized to 0 in vitro, the inside of the body of the function built-in type variable is not initialized, is undefined, the class object is initialized to determine their own way, as default initialization string is an empty string.
  3. Variable names in lower case, class names begin with an uppercase, multiple words connected with underscores.
  4. When the variable is first used in its previously defined variables.

2.3 Complex Type

Fundamental contents:

  1. That reference to the alias, not the object must be initialized , and can only be bound to an object, can not bind a literal or expression evaluates to type with their type bound object references must match exactly (a reference to the constant and the base reference to the class exception).
  2. Pointer is a "point to" another type of match type. Value of the pointer can point to an object, the object may be directed close to the space occupied by the next position may be a null pointer or an invalid pointer. Pointer type pointing object type it must match exactly (and a pointer to a pointer to the exception base class constants). void * pointer can be stored in any type of pointer.
  3. Statement defines a plurality of variables, each variable but note type; bound reference pointer (int * & pi); multiple pointer (int **).

Remarks:

  1. Elementary data type declaration statement + = descriptor list declaration.
  2. References and pointers to achieve indirect access to other objects, but the reference is not an object, a pointer is an object; references must be initialized, the pointer without having initial values ​​when defining; references can only be bound to an object, the pointer can point to different objects.
  3. Initialize all pointers.
  4. There are references to binding pointer (int * & pi), but the pointer does not point to a reference, because the reference is not an object.

2.4 const qualifier

Fundamental contents:

  1. const define constants must be initialized , const objects can not be modified, the default is only valid within the file.
  2. Type references must be bound with the same type of object it. Exception: As long as the expression can be converted into the type of reference, references to it allows constant as an initial value.
  3. Pointer type to be consistent with the type of object it points. Exceptions: Allow constant pointer to point to a corresponding non-constant; itself const pointer must be initialized.
  4. I.e., the top-level object itself const are constants, i.e., the bottom pointer const or referenced objects binding is constant.
  5. constexpr variables must be a constant, must be initialized with a constant expression; only literal types (arithmetic types, reference, pointer) can be defined as constexpr; constexpr only valid pointer, the pointer to the object is invalid

Remarks:

  1. External file called const constant, the need for constant const declarations and definitions are added extern.
  2. A reference constant (const int &); pointer pointing constant (const int *), itself is a constant pointer (int * const p); reference pointer (int * & ri).
  3. You must be initialized types: reference, const, auto.

2.5 Processing Type

Fundamental contents:

  1. Type alias typedef, define the type without defining variables. When the type of alias reference, pointer mix, the type of alias as a whole, can not replace the original type.
  2. auto type specifier can calculate the initial value of the variable the variable type, auto definitions must be initialized . When the expression is a reference, auto type is the type of reference that object for binding, auto ignored the top-level const, leaving only the underlying const.
  3. delctype and returns the data type indicator to select the type of operand, the operand if in brackets, the reference will always be delctype type.

Remarks:

  1. A statement to define a plurality of variables, and * & subordinate only subsequent specifier, rather than the front of the basic data types.
  2. The difference between auto and delctype: delctype return operands of type, if contains parentheses, it returns a reference; auto type is generally the type of expression, if quoted, auto is the type of object bound references, if const, ignoring the auto top const, to retain the underlying const.

Custom data structure 2.6

Fundamental contents:

  1. Defining and using Sales_data type.
  2. Edit header files, compile, run.
  3. Preprocessor header guard

Remarks:

  1. Definition of the class object is best not to put together.
  2. Available data members in the class initialization brace and an equals sign, you can not initialize parentheses.
  3. Header files generally have a protective symbol, protection symbol name is constructed from the class name, to be in all caps.
Published 77 original articles · won praise 25 · views 10000 +

Guess you like

Origin blog.csdn.net/qq_34801642/article/details/103919588