C++ Primer Plus Notes (Chapter 9)

1. When including your own header file, use "" instead of <>, such as "coordin.h"; the file name is in "", the compiler will first search in the current working directory or source code directory.

  <iostream>, <> means that the C++ compiler will search in the file system of the host system where the standard header files are stored.

2. The same header file can only be included once in the same file

  #ifndef  COORDIN_H_

 #define COORDIN_H_

....

#endif

3. Automatic storage persistence: (Automatic variables) The storage persistence of variables (including function parameters) declared in the function definition is automatic. They are created when the program starts to execute the function or code block to which it belongs, and the function is executed after the function is executed. Or code blocks, the memory they used is released. The memory occupied by automatic variables is called the stack. After the memory is released, the value is not deleted, but is not marked, and the top of the stack is restored to its original position . (Code block refers to the statement contained in curly braces {})

Static storage persistence: (static variables) The storage persistence of variables defined outside the function definition and variables defined with the keyword static is static, and exists throughout the entire running process of the program. The compiler will allocate fixed memory to store static variables.

Dynamic storage persistence: (Dynamic variables) The memory allocated with the new operator remains in place until it is released with the delete operator or the program ends. This storage persistence is dynamic and is sometimes called free storage or heap .

4. There are many scopes of C++ variables,

  The scope of a C++ function can be the entire class or the entire noun space (including global), but it cannot be local (functions cannot be defined in code blocks).

5.int main ()

{

int tel; ...... // tel # 1

{int tel;.....} //tel #2, tel #1 is hidden and invisible in this {} code block, and tel #1 is visible again after executing the code block.

}

6. Keyword register: It is recommended that the compiler use CPU registers to store automatic variables .

7. Three Linkages of Static Persistent Variables 

int global=1000; //global is declared outside the code block and has external linkability. Other files of the program can use global;

  static int one_file=50l; //one_file is declared outside the code block and uses the static qualifier, which has internal linkability. One_file can only be used in the file containing the above code

int main () {...}

void fun1 (int n){

static int count=0; // count is declared in the code block, and static qualifier is used, no linkability

int lama=0; //Static variable count is similar to automatic variable and can only be used in fun1 () function. The difference is that even if the fun1 () function does not execute count, it occupies memory

}

void fun2 (int q){ ....}

8. All bits of static variables that are not initialized are set to 0;

The initialization of static variables is divided into: static initialization and dynamic initialization. Static initialization (zero initialization and constant expression initialization)

Static initialization: initialize const double pi=4.0*atan(1.0); 

9. Variables whose linkability is external are called external variables. External variables can be used by multiple files in the program, but each file that uses external variables must be declared; but variables can only be defined once. Therefore there are two declarations of C++:

Definition statement: (definition) allocate storage space for variables

Reference statement: (Declaration) Do not allocate storage space, use the keyword extern, do not initialize, if initialized becomes a definition, allocate storage space

extern int blem; //Declaration, blem is defined elsewhere

extern char gr='z'; // define gr, allocate storage space double up; // define up

10. Use const global variable linkability as internal. But you can use the extern keyword to override the default internal linkability.

extern const int state=34; //External linkability

11. The storage persistence of all functions is static. By default, the linkability of functions is external. You can use extern (or not) to indicate that the function is defined in another file. You can also use static to set the function linkability to internal so that it can only be used in one file.

12. Use new initialization: int * ar=new int[4] {2,6,7,3};

 Use new and new[] to call functions:

void * operator new(std::size_t); //size_t is a typedef, corresponding to the appropriate integer type. Release function: void operator delete(void *)

void * operator new[] (std::size_t); Release function: void operator delete [] (void *)

Position the new operator:

char buffer[500];

p1=new (buffer) int[20] ;// Place the Int array in the buffer.

There is no need to delete [] p1; because the buffer is outside the jurisdiction of delte.

13. Create the namespace keyword namespace, the name in any namespace will not conflict with the names of other namespaces (different namespaces can use the same name)

namespace Jack{ double pail; void fetch(); int pal; } 

The name of the access namespace uses the scope resolution operator:: such as Jack::pail=12.34; the name that contains the namespace Jack::pail is called the qualified name

14. The using statement makes the specific identifier available: using Jack::fetch ; After completing the statement, fetch can be used instead of Jack::fetch in the valid area

The using compiler directive makes the entire namespace available : using namespace Jack; all names in the namespace are available without the scope resolution operator:: 

15. The unnamed namespace can replace the linkability as internal static variables:

Declaration outside the function: static int counts;

Can be written as a declaration outside the function: namespace {int counts;}

16. Call the average(3,6) function in a file to return the int average value of the two int parameters, and call it in another file in the same program, it returns the double average value of the two int parameters. Implementation method:

Include a separate static function definition in each file, or each file defines an appropriate average() function in an unnamed namespace (15 above)

   

 

Guess you like

Origin blog.csdn.net/lvliang2017232003/article/details/85990362