'C++ primer plus' reading notes of three

  • 2020-04-02 02:51:10
  • OfStack

  Chapter 9

1, C++ program composition

(1) header file: contains structure declarations and prototypes that use these structures.

(2) source code file: contains the code of structure-related functions.

(3) source code file: contains the code to call structure-related functions.

2. Header file --

(1) contents usually included: function prototype; Sign constant defined by #define or const; Structural declaration; Class declaration; Template declaration; Inline function.

(2) if the file name is included in Angle brackets, the compiler looks it up in the file system of the host system where the standard header file is stored.

(3) if the file name is contained in double quotes, the compiler first looks for the current working directory or source directory, and then in the standard location.

(4) the same header file can only be included once in the same file. Used to ignore everything except the first inclusion.

In the header file coordin.h, #ifndef COORDIN_H_define is commonly used   COORDIN_H_ # endif

3, the compilation process --

(1) command: CC file1.cpp file2.cpp

(2) the preprocessor merges the included files (such as header files) with the source code. Generate temporary files temp1.cpp and temp2.cpp

(3) the compiler creates object code files for each source code file :file1.o and file2.o

(4) the link program combines the object code file, library code and startup code to generate an executable file: a.out

(note! When linking compilation modules, make sure that all object files and libraries are generated by the same compiler.

4. Storage persistence -- automatic storage persistence, static storage persistence, thread storage persistence (C++11), dynamic storage persistence.

5. Stack -- the data in the stack is in the adjacent memory unit

(1) trace the stack with 2 Pointers, one to the bottom of the stack (the starting position of the stack), one to the top of the stack (the next available memory unit)

(2) the new value is not deleted, but is no longer marked.

6. Linkability --

(1) external linking, can be accessed in other files. For example, outside the function, do not use the variable defined by staitic

(2) internal linking, which can only be accessed in the current file. For example: function outside the static definition of the variable

(3) no linking, can only be accessed in the current function or code block. For example, a variable defined in a code block

(note! All static variables are initialized by zero first, and then dynamically and statically.

7. Variable declaration --

Single definition rule - > Variables can only be defined once

(1) definition declaration (definition) : allocate storage space to variables.

(2) reference declaration (declaration) : no space is allocated, referring to existing variables. Use the keyword extern without initialization.

(note! To use external variables in multiple files, define them in one file and use extern declarations in other files.

(use extern to override the default internal linkability of a variable to make it external)

Const char * const a[12] = {" a "," b","c"... }; // the first const prevents strings from being modified, and the second const ensures that each pointer in the array points to the string it originally pointed to.

  The type of this pointer is char * [12], which is an array of Pointers of type char with an array size of 12. The first const modifies char *, the second const modifies a[12]. So.

9. Specifiers and qualifiers --

(1) keyword thread_local: indicates that the persistence of a variable is the same as the persistence to which it belongs.

(2) const: after the memory is initialized, the program cannot modify it. Const global variables are interlinked.

(3) volatile: value is taken every time, value optimization is not allowed.

(4), the mutable: even structure for const variable (or class), a member is mutable modified can be modified.

Function link-all function storage persistence is automatically static, link-ability is external. You can use static to make linkability internal in stereotypes and definitions.

Use new to initialize --

Int a = new int(5); // set the value of a to 5.

(an exception STD ::bad_alloc is thrown when new fails to find the amount of memory requested.)

Void * operator new (STD ::size_t);

(2), operator new[] actually calls void * operator new[] (STD ::size_t); // here STD ::size_t is actually an alias typedef for an appropriate integer.

12. Locate the new operator --

Can specify memory location to use. It does not track which memory units are unused or look for unused memory blocks.

(1) such as: #include < The new > Char buffer [50]. Struct1 * s = new (buffer) struct1; // allocate memory of struct1 size from buffer.

13. Namespace --

This is equivalent to a package in Java, but there are many differences.

(1) a namespace can be global or in another namespace.

(2) by default, the linkability of names declared in namespaces is external.

(3) global namespace, corresponding to the file level declaration region, where the global variable is located.

(4) namespace is open, that is, some names can be added to the existing namespace, such as namespace QSK {char* name(const char*); }// add this name to QSK.

(5) define each name by scope operator, such as: cout < < QSK: : name < < Endl;

Using declaration and using compilation instruction --

(1) the using declaration makes specific identifiers available. Using STD ::cout; using STD ::cout;

(2) the using compilation instruction makes the entire namespace available. (add a namespace) : using namespace STD;

Note that you do not use using to compile instructions in header files. For the using declaration, it is preferred to scope it locally rather than globally.

  Chapter ten

15, -

(1) class members can be data or functions. When a class is declared, it is decorated with an access control character.

(2) when defining a member function, use the scope resolution operator (:) to indicate the class to which the function belongs. Such as void Stock: : update (double price) {}

(it can access private members of the class)

(3) define the function in the class declaration and automatically become an inline function.

(4) you can also make the function inline outside the class, just use the inline qualifier in the class implementation, such as: inline void Stock::update(double price){}

(5) create objects, such as: Stock a,b; You can also use new to allocate storage space for an object. Stock a = new Stock;

(6) use the member function through the member operator, such as: a. how();

(7) each object created has its own storage space for storing its internal variables and class members; All objects share a set of class methods.

Access control -- private, public, protected

There is no need to use the keyword private in the class declaration, which is the default access control for the class.

(note! In C++, structures have the same characteristics as classes, except that the default access type for structures is public.

17, class design --

(1) provide class declaration.

(2) implement the class member function. Function definitions are usually provided separately, with (::) specifying which class the function belongs to.

Class constructor --

Data members dedicated to constructing new objects and assigning values to them.

(1) the constructor prototype and the header return no value and are not declared as type void. The constructor does not declare a type.

(2) the parameters of the constructor represent not the class members, but the values assigned to them, so the parameter names cannot be the same as the class members. (unlike Java)

  (it is common to use the m_ prefix or suffix _ for data member names.)

19. Use the constructor --

Stock s("a", 22, 1.2); Or Stock a;

Stock s = Stock("a", 22, 1.2);

  (note that constructors have other characteristics similar to Java.)

20. Destructor -- a member function that is automatically called by the program when an object expires to complete the cleanup. Such as: ~ Stock ();

(note! If an object is created with new, its destructor is automatically called when memory is freed with delete.

Object assignment -- by default, when an object is assigned to another object of the same type, the contents of each data member in the source object are copied to the target object.

(1) initialization method: Stock s = Stock("a", 22, 1.2); // temporary variables may be created.

(2) assignment method: s1 = Stock("a", 22, 1.2); // always create a temporary variable before the assignment. Destructors are automatically called for temporary variables.

22. Const member function -- determines the class method that does not modify the object. Const keyword is placed after the function parenthesis, such as: void stock:: show() const;

C++11 list initialization -- for Stock jock {"abcd"} will match Stock::Stock(const STD ::string & co, long n = 0,double pr = 0.0);

For Stock c {}; The default constructor will be matched.

24, this pointer -- used in class methods, this pointer to the calling object, is the address of the object calling the class method. If you want to refer to the entire calling object, you can use *this.

25, an array of objects, such as: Stock s [4] = {Stock (" a ", 22, 1.2), the Stock (), Stock (" a ", 22, 1.2)};

// the default constructor is used to create the array elements, the constructor in curly braces creates the temporary object, and the contents of the temporary object are copied into the corresponding element

(note! Creates an array of class objects, which must have a default constructor.

26, scope for the class constant --

(1) the enumeration declared in the class, scope for the whole class, does not belong to the object, but belongs to the class.

(2) constants decorated with the keyword static will be placed in the static storage area, not belonging to the object.

  27. New enumeration -- enum class or enum struct

(1) scope of enumerators is class. You need to qualify the enumerator with the enumeration name class.

(2) conventional enumeration is automatically converted to integer, but in-scope enumeration cannot be implicitly converted to integer. But you can do it explicitly.

(3) the underlying type of the new enumeration is int, but the underlying type can also be specified, such as: enum class: short pizza {a, b, c, d};

28. Summary --

(1) class combines data and methods into a unit, and its privacy realizes data hiding. Classes are user-defined types and objects are instances of classes.

(2) class declaration should be placed in the header file, and the source code defining the member function should be placed in the method file.

(3) each object stores its own data and shares class methods.

(4) Abstract Data Type -- ADT


Related articles: