C + + Programming thought (Volume one) Read summary

Source: Internet
Author: User

Recently work idle read the previous buy F "C + + programming thought" here, I think the most helpful part, one left to review their own, on the other hand, and share with the small partners:

1 OPP-related

1.1, each object has a type, oop in the class and type is synonymous, in the object-oriented program design, our work is actually to create a new data type, the programmer defines class is to adapt to the specific problem, rather than forced to apply the existing data types.

1.2, when the use of <> to specify the file, the preprocessor in a specific way to find the file, usually in the environment or a compiler command to specify a search path, when the "" "Specify the file, usually start from the current directory to find, if the file is not found, press <> search.

2 C + + total

2.1, integer type range from small to large for: short Int->int->long Int.

Floating-point type range from small to large: Float->double->long double;

The unsigned number does not save the symbol, so there is an extra bit available, so it can store an integer twice times larger than the signed number.

2.2. The internal and external variables defined in main () are stored in different regions, and data and functions exist in different regions.

2.3, the general principle of C + + programming is to initialize at the time of definition, the const variable must have the initial value

2.4, enum can improve the program clarity, the use of Union can save memory, all union variable address is the same

2.5, the use of pre-processor to complete the code debugging functions, the specific implementation of the following:

#define NDEBUG//... #ifdef ndebug/* debugging code here*/
... #endif
2.6. C + + allows any type of pointer to be assigned to void*, but does not allow the void* z pointer to be assigned to any other type of pointer.

2.7, the same file is the contract between us and the library user, the contract describes the data structure in the library, the parameters and return values are specified for the function call, the header file is limited to the declaration, in order to avoid the contents of the header file multiple declarations, we can use the following ways to avoid:

#ifndef header_flag#define Header_flag//type declaration here ... #endif
Also in order to avoid namespace confusion, the header file will never see the using

2.8. If the programmer wants to allow a member function that does not belong to the current result to access an object of the current structure, you can use the friend modifier, friend must declare within a struct

3. C + + Features:

3.1, the difference between class and struct is that the member of class is private by default, and the member of struct is public by default;

3.2, the security of C + + is embodied in the initialization and elimination of two aspects, with the constructor to ensure initialization, with the destructor to ensure the elimination of

3.3. Macros in C + + are replaced by inline functions (inline function), which ensures high efficiency (both member functions and preprocessor macros are as efficient) as well as security (when an inline function is called, the compiler first ensures that the call is correct, that all parameter types are met, that the macro is defined in preprocessing, cannot be checked).

Any function defined in the class automatically becomes an inline function;

The purpose of using inline functions is to reduce the pleasure of function calls, but if the function is big, because it needs to repeat the assignment code at every point in the calling function, it expands the code, so the small function is ideal as an inline function

3.4. Static causes the variable to be stored in the static data area instead of on the stack. The constructor for the global static object is called before main () and the destructor is called after exiting Main ();

3.5. Keyword namespace How class, struct, enum, and union put their members ' names in different spaces, but namespace differs from class, struct, union, and enum in a distinct way:

(1) Namespace can only be defined in the global scope, but they may be nested with each other;

(2) The end of the namespace definition does not have to follow the semicolon;

(3) A namespace can be defined in multiple header files with an identifier;

(4) Cannot create an instance of a name like class;

(5) Namespace may use another name as his alias;

3.6, & in C + + for reference, reference can be understood as a strange pointer, the advantage of this pointer is not to doubt whether it is initialized, the use of the reference rules are as follows:

(1) Application creation must be initialized;

(2) A reference to an object that cannot be converted to another object;

(3) A reference that cannot be null

The most frequently seen reference is in the function parameter and the return value, when the reference is used as a function parameter, any modification of the referenced object within the function will change outside the function parameters, of course, the pointer can also be passed to do the same thing, but the reference has a clearer syntax.

3.7. Copy constructor (Copy-constructor) is a special constructor that requires references to implement new objects from existing objects of the same type. The compiler uses copy constructors to pass and return objects in a function by value passing

3.8. Using delete void * may make an error, it must be converted to the appropriate type before deletion, otherwise the memory will be lost;

3.9. C + + ensures that the other constructors of the ghee are called before entering the constructor body of the new class, so that any calls made by the member functions of all child objects are always transferred to the initialized object.

Mytype::mytype (int i): Bar (i) {///...}
4 polymorphism in C + +

4.1. Polymorphism always occurs between the derived class and the base class, C + + is implemented by the virtual function and late binding (late binding);

If a function is declared as virtual in the base class, then all of the derived classes are virtual and can be overridden by a virtual function in the base class;

In each class with a virtual function, the compiler secretly places a pointer (vpointer) to the VTABLE of the object (the compiler creates a table for each class that contains virtual functions: VTABLE)


The compiler starts with the instrument pointer, which points to the starting address of the object, and for all instrument or derived objects, their vptr all exist in the same position (often at the beginning of the object), so the compiler can take out the vptr of the object. Vptr points to the starting address of the vtable, and all vtable have the same order. Play () The first one, what () 2nd;

5, C + + templates (template)

5.1, the template tells the compiler, and then the definition will be wrong to do one or more unspecified types, when the templates produce the actual class code, you must specify these types so that the compiler can replace them.



C + + Programming thought (Volume one) Read summary

Related Article

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.