C ++ study notes (3)

This study notes that C ++ primer plus (sixth edition) study notes. Is C ++ study notes (2) follow. Review the basics of C ++ can Miao Miao.

Please indicate the source http://www.cnblogs.com/zrtqsk/p/3881141.html, thank you! as follows.

 

 Chapter nine

 

1, C ++ program component -

  (1) Header: comprising a structural prototype declaration and use of these structures.

  (2), the source code files: contains the code of the functions associated with the structure.

  (3), the source code files: contains the code to call functions associated with the structure.

 

2, header files -

  (1), often include content: The function prototype; #define or symbolic constant const defined above; declaration structure; class declaration; template declaration; inline functions.  

  (2) If the file name enclosed in angle brackets, the compiler will look in the host system memory standard header file in the file system.

  (3) If the file name in double quotation marks, the compiler first searches the current working directory or source code directory, and then find in the standard position.

  (4), the same file can only contain the same header file once. To ignore all but the first time it contains.

  Coordin.h header files, often use: #ifndef COORDIN_H_ define COORDIN_H_ / * header file contents * / #endif

 

3, the compilation process -

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

  (2), comprising a pre-processor file (file header or the like), and the source code merger. Generate temporary files temp1.cpp and temp2.cpp

  (3), the compiler source code file is created for each object code file: file1.o and file2.o

  (4), the linker will object code files, boot code and library code merge, generate executable file: a.out

  (Note! No links compiled modules, make sure that all object files and libraries are the same compiler-generated.)

 

4, persistent storage - continuous automatic storage, static storage persistent, continuous thread storage (C ++ 11), dynamic storage continuity.

 

5, the stack - the stack data in the adjacent memory cells

  (1), using two stack trace pointers, pointing to a stack bottom (the start position of the stack), pointing to a stack (the next available memory cell)

  (2), the new value is not deleted, but will not be marked.

 

6, link sex -

  (1), external links, accessibility in other documents. Such as: external function, without using a variable defined staitic

  (2), of the internal links, can only be accessed in the current file. Such as: external function, using the static variable defined

  (3), there is no link property, or function can only be accessed in the current block. Such as: a variable defined in the code block  

  (Note! All the static variables are initialized to zero first, and then select the dynamic and static initialization.)

 

7, variable declaration -

  Single-defined rules -> variable is defined only once

  (1), defining declaration (definitions): allocate storage space for the variable.

  (2), quoted a statement (statement): not allocate space, reference existing variables. Use the keyword extern, not initialized.

  (Note! To use an external variable in multiple files, should be defined in a file, other files using extern declaration.)

  (Using extern variables can override the default internal links, making it become external)

 

. 8, const char const * A [12 is] = { "A", "B", "C" ...};  // const prevent the first string is modified, the second array to ensure that each pointer const it was originally pointed to a string.

   This type is char * pointer [12], i.e. the array, the array size of a pointer is 12 char type. The first modification const char *, const second modified a [12]. and so.

 

9, specifiers and qualifiers -

  (1) keyword thread_local: indicate continuity of the same continuous variables and to which it belongs.

  (2), const: the memory is initialized, the program can not modify it. Links of const global variables as internal.

  (3), volatile: every value, does not allow the value of optimization.

  (4), mutable: even though the structure (or class) variable is const, is a mutable modified members can also be modified.

 

10, the link of function - storing persistent all functions are automatically static, the link of the external. Static and can be used in the prototype will be defined as a set of inside links.

 

11, using a new initialization -

   int a = new int (5); // set below a value of 5. Comparative braces initialization more convenient and versatile.

  (The amount of memory can not find new requests, failure, throws an exception std :: bad_alloc)

  (1), new operator actually calls void * operator new (std :: size_t);

  (2), the operator new [] actually calls void * operator new [] (std :: size_t); // std :: size_t where actually a typedef alias, corresponding to a suitable integer.

 

12, positioning the new operator -

  You can specify the memory location to be used. Do not keep track of which memory cells are not used, nor to find an unused memory blocks.

  (1): #include <new> char buffer [50]; struct1 * s = new (buffer) struct1; // allocate memory from struct1 size buffer.

 

13, the namespace -

  Java equivalent of the package, but there are a lot different.

  (1), it may be a global name space may be located in another namespace.

  (2), by default, the link name namespace declaration is outside.

  (3), the global namespace, file-level corresponds to the area declarations, global variables are located here.

  (4), the name space is open, i.e., certain names may be added to an existing namespace, such as: namespace qsk {char * name (const char *);} // add the name to QSK.

  (5), is defined by the scope of each operator name, such as: cout << qsk :: name << endl;

 

14, using declarations and using compiler directives -

  (1), using a statement that the specific identifier is available. (No name is defined, used as received), such as: using std :: cout;

  (2), using the name of compiler directives such that the entire space available. (More than a namespace), such as: using namespace std;

  (Be careful not to use using compiler directives in the header file for using the statement, which was the preferred scope to local rather than global.)

 

 chapter Ten

 

15, class -

  (1), it may be a class member may be a function of the data. When the class declaration, with access control characters to describe.

  (2), the definition of the member function, using the scope resolution operator (: :) to represent the class which the function belongs. The void Stock :: update (double price) {}

  (It can access private members of the class)

  (3), the definition of the function is located in the class declaration, the automatic inline function.

  (4), may be made outside the class function inline function, only need to use inline qualifier class implementation, such as: inline void Stock :: update (double price) {}

  (5), to create an object, such as: Stock a, b; may also use the new memory space allocated for the object. Such as: Stock a = new Stock;

  (6) to a member function member by operators, such as: a.show ();  

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

 

16, access control --private, public, protected

  Without the use of private keyword in the class declaration, it is the default type of access control.

  (Note! C ++, the class structure has the same characteristics, but the default access type structure is public.)

 

17, class design -

  (1), provide the class declaration.

  (2), the implementation class member functions. Typically provide separate function definitions, (: :) to develop the function belongs to which class.

 

18, class constructor -

  Specifically used to construct the new object, assign a value to their data members.

  (1), and a prototype head constructor function does not return a value, and has not been declared void type. Constructors do not have a declared type.

  (2) it is not a member of the class constructor parameter representation, but given their value, parameter name can not be the same as the class members. (Unlike Java)

   (Common practice is to use the m_ prefix data member name, or use the suffix _)

 

19, using a constructor -

  (1), an implicit call: Stock s ( "a", 22, 1.2); or Stock a;

  (2), an explicit call: Stock s = Stock ( "a", 22, 1.2);

   (Note that other features of the constructor similar to Java.)

 

20, destructor - when the object expires, the program automatically calls the member function to complete the cleanup work. Such as: ~ Stock ();

  (Note! If a new object is created by using the delete release memory when its destructor is automatically invoked.)

 

21, the object assignment - By default, when an object is assigned to the other objects of the same type, the contents of each data source object members will be copied to the target object.

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

  (2), assignment mode: s1 = Stock ( "a", 22, 1.2); // Create a temporary variable is always before the assignment. And automatically calling the destructor for the temporary variable.

 

22, const member functions - determining method does not modify the object class. const parentheses after the function key, such as: void stock :: show () const;  

 

23, C ++ 11 initializes the list - For Stock jock { "abcd"} will match Stock :: Stock (const std :: string & co, long n = 0, double pr = 0.0);

  For Stock c {}; matches the default constructor.

 

24, this pointer - class used in the method, the this pointer to the call object, the object class method calls the address. To quote the entire call object, you can use * this.

 

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

    // Here First create array elements using the default constructor, and then create a temporary object constructor flower brackets, then the contents of the temporary object is copied to the corresponding elements

  (Note! Create an object array class, the class must have a default constructor.)

 

26, scoped class constants -

  (1), the enumeration declared in the class, the scope for the entire class, not part of the object, but belong to the class.

  (2), use the keyword static modification of the constants will be placed in static storage area, are not covered.

 

 27, the new enumeration --enum class or enum struct

  (1), the amount of the scope is enumerated classes. Enumeration enum defines the amount required by the class name.

  (2), the conventional enumeration automatically converted to an integer, but the scope is not enumerated implicitly to an integer. But it can explicitly convert.

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

 

28 Summary -

  (1), the class data and methods combined into one unit, which privacy for data hiding. Class is a user-defined type, the object is an instance of the class.

  (2), a class declaration in the header file, the member functions defined in the source code should method file.

  (3), each object stores its own class data sharing method.

  (4), abstract data types (Abstract Data Type) - ADT

 

 

    

 

Reproduced in: https: //www.cnblogs.com/zrtqsk/p/3881141.html

Guess you like

Origin blog.csdn.net/weixin_34259159/article/details/93248541