Nineth Chapter
1, C + + program composition--
(1), header file: Contains the structure declaration and uses these constructs the prototype.
(2), source code file: Code that contains a structure-related function.
(3), source code file: Contains code that invokes a struct-related function.
2, header file--
(1), commonly included content: function prototype; #define或const定义的符号常量; struct Declaration; class declaration; Template declaration; inline function.
(2) If the filename is contained in angle brackets, the compiler will look in the file system of the host system that stores the standard header files.
(3) If the file name is enclosed in double quotes, the compiler first looks for the current working directory or source code directory, and finds it in the standard location.
(4) Only the same header file can be included once in the same file. Used to ignore everything except for the first time.
Header file Coordin.h, often used: #ifndef coordin_h_ define coordin_h_/* Header File Content * * * #endif
3, the compilation process--
(1), Order: CC file1.cpp file2.cpp
(2), the preprocessor will contain the files (such as header files) and source code merge. Generate temporary files Temp1.cpp and Temp2.cpp
(3), the compiler creates the target code file for each source code file: FILE1.O and FILE2.O
(4) The linker will merge the target code file, library code and startup code to generate the executable file: a.out
Attention When you link a compile module, make sure that all object files and libraries are generated by the same compiler. )
4, storage continuity-automatic storage persistence, static storage persistence, thread storage persistence (C++11), dynamic storage persistence.
5, Stack--the data in the stack in the adjacent memory unit
(1), using 2 pointer tracking stack, a point to the bottom of the stack (the beginning of the stack), one point to the top of the stack (the next available internal deposit)
(2), the new value has not been deleted, but is no longer marked.
6, the link sex--
(1), external link sex, can be accessed in other files. such as: Outside the function, do not use staitic-defined variables
(2), internal link sex, only in the current file access. For example: Outside a function, a variable defined using static
(3), no link, can only be accessed in the current function or block of code. such as: variables defined in the code block
Attention All static variables are initialized first by 0, and then dynamic, static initialization is selected. )
7, variable statement--
A single definition rule-> variable can only be defined once
(1), definition Declaration (definition): Allocate storage space to variables.
(2), Reference Declaration (Declaration): Do not allocate space, reference existing variables. Uses the keyword extern and does not initialize.
Attention To use external variables in multiple files, you should define them in one file and use an extern declaration in other files. )
(use extern to override the default internal link of a variable to make it external)
8. const char * Const A[12] = {"A", "B", "C" ...}; The first const prevents the string from being modified, and the second const ensures that each pointer in the array points to the string it initially points to.
The type of this pointer is a char * [12], an array of pointers of type char, with an array size of 12. The first const modifier char *, the second const modifier A[12]. So.
9, descriptors and qualifiers--
(1), keyword thread_local: Indicates the continuity of the variable and the continuity of the same.
(2), const: After the memory is initialized, the program cannot modify it. The linkage of the const global variable is internal.
(3), volatile: Each time the value, do not allow value optimization.
(4), mutable: Even if the struct (or class) variable is const, a member modified by mutable can also be modified.
10, the function of the link-all functions of the storage continuity is automatically static, the link is external. You can use static to set the linkage to internal in the prototype and in the definition.
11, using new initialization--
int a = new int (5); Set A's value to 5. In comparison, curly braces are more convenient and versatile to initialize.
(New cannot find the requested amount of memory, and when it fails, an exception will be thrown Std::bad_alloc)
(1), operator new actually calls 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, corresponding to the appropriate integer type.
12, locate the new operator--
Can specify the memory location to use. does not track which memory cells are unused and does not look for unused memory blocks.
(1) such as: #include <new> char buffer[50]; Struct1 * s = new (buffer) Struct1; Allocates the Struct1 size memory from the buffer.
13, the name space--
It's equivalent to package in Java, but there are a lot of differences.
(1), the namespace can be global, or it can be in another namespace.
(2), by default, the link nature of the name declared in the namespace is external.
(3), global namespace, corresponding file-level declaration area, where the global variable is located.
(4), the namespace is open, that is, some names can be added to an existing namespace, such as: namespace Qsk {char * name (const char*);} Add this name to the QSK.
(5), through the scope operator to qualify each name, such as: cout << qsk::name << Endl;
14, using declaration and using compilation directives--
(1) A using declaration makes a specific identifier available. (without qualifying name, direct use) such as: using Std::cout;
(2), using compilation directives make the entire namespace available. (one more namespace) such as: using namespace std;
(Note that you do not use a using compilation directive in the header file.) For a using declaration, the preference is to set its scope to local rather than global. )
Tenth Chapter
15, Class--
(1), a class member can be a data or a function. class declaration, it is decorated with an access controller.
(2), when defining a member function, use the scope resolution operator (::) to represent the class to which the function belongs. such as void stock::update (double price) {}
(It can access private members of the class)
(3), the function defined in the class declaration, automatically becomes an inline function.
(4), or you can make functions inline functions outside of the class, simply use the inline qualifier in the class implementation, such as: inline void stock::update (double price) {}
(5), the creation of objects, such as: the stock a,b; You can also use new to allocate storage space for an object. Such as: stock a = new stock;
(6), using member functions through member operators, such as: A.show ();
(7), each object created has its own storage space for storing its internal variables and class members, and all objects are common to a set of class methods.
16, access control--private, public, protected
You do not have to use the keyword private in the class declaration, which is the default access control for the class.
Attention In C + +, structs have the same attributes as classes, but the default access type for structs is public. )
17, class design--
(1), provide class declaration.
(2), implementation class member function. The function definition is usually provided separately, by (::) to define which class the function belongs to.
18, the class constructor--
A data member that is specifically used to construct new objects and assign values to them.
(1), the constructor's prototype and function headers have no return values, and are not declared to be of type void. Constructor has no declared type.
(2), the parameters of the constructor represent not the class members, but the values given to them, so the parameter names cannot be the same as the class members. (Different from Java)
(common practice is to use the m_ prefix in the data member name, or use the suffix _)
19, the use of constructors--
(1), implicit call: Stock S ("a", 22, 1.2); or stock A;
(2), explicit call: the stock s = the stock ("a", 22, 1.2);
(Note that other features of the constructor are similar to Java.) )
20, destructor-when the object expires, the program automatically calls the member function, to complete the cleanup work. such as: ~stock ();
Attention If an object is created by new, and the memory is freed with delete, its destructor is invoked automatically. )
21, 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 way: the stock s = the stock ("a", 22, 1.2); Temporary variables may be created.
(2), assign value way: S1 = Stock ("A", 22, 1.2); You always create a temporary variable before assigning a value. And the destructor is automatically called for the temporary variable.
22, const member function-determines the class method that does not modify the object. The Const keyword is placed behind the function parentheses, such as void Stock:: Show () const;
23, c++11 list initialization-for the 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 points to the calling object, the address of the object that invokes the class method. You can use *this if you want to refer to the entire calling object.
25. Array of objects-such as: stock s[4] = {The stock ("a", 1.2), the stock (), the stock ("a", 22, 1.2)};
This first creates an array element using the default constructor, and then creates a temporary object with the constructor in parentheses, and then the contents of the temporary object are copied into the corresponding element
Attention Creates a class object array, which must have a default constructor. )
26, scope is a constant of class--
(1), the enumeration declared in the class, the scope is the entire class, not belong to the object, but belongs to the class.
(2) Constants decorated with the keyword static are placed in a static storage area and are not part of the object.
27. New enumeration--enum class or enum struct
(1), the scope of the enumerator is class. You need to qualify the enumerator with the enumeration name class.
(2), general enumerations are automatically converted to integral types, but scopes within the scope cannot be implicitly converted to integral types. However, you can explicitly convert.
(3), the underlying type of the new enumeration is int, but you can also specify the underlying type, such as: Enum Class:short Pizza {A, B, C, d};
28, summary--
(1) The class combines data and methods into one unit, and its privacy realizes data hiding. A class is a user-defined type, and an object is an instance of a class.
(2), the class declaration should be placed in the header file, the definition of the member function source code should be placed in the method file.
(3) Each object stores its own data and shares the class method.
(4), abstract data type (--adt)