High Quality C ++ Program Design Guide Abstract (1) (Original)

zhaozj2021-02-12  138

Good programming habits

one type:

Package: Let the module only have to let the outside world know all the contents.

Inheritance: 1. If the Class A and Class B are not associated, the function of B is inherited to inherit a function and attributes.

2. If logically b is one of A, all features and attributes of A are meaningful to B, allow B

Inherit a function and attributes.

Combination: If logically A is part of B, B is not allowed to derive from A, but combined with A and other things B.

Virtual function: Once a function of a class is declared as a virtual function, the corresponding function of its derived class is also virtual function. It is recommended that the explicit statement in each layer is a virtual function.

Abstract base class: Virtual void Draw (void) = 0; then DRAW () is a pure virtual function. The class containing the pure virtual function is a member function as an abstract base class.

Dynamic Edge: Call the virtual function with the base class pointer to the derived class object, the program will dynamically select the virtual function of the faction, this feature is called dynamic.

Polymorphism: Many derived classes have a relationship because of inheriting a common base class, each of which can be used as an object of a base class, which can be used to make different reflections for the same function. It is polymorphism. Polymorphism is achieved by virtual functions and dynamic cable.

Header file: 1. In order to prevent the header files from being reused, use the IFNDEF / DEFINE / ENDIF structure to generate a preparation block.

2. Use # include

Format to reference the standard library's header file, the compiler will start searching from the standard library directory.

Use the # include "filename.h" format to reference the header file of the non-standard library, the compiler will be from the user

Take a directory start search.

3. Only store the declaration in the general header file, not deprecating.

4. Members of the class can be defined when declaring and become inline functions. Inline function can improve functions

Performance efficiency, but expand code, not recommended.

Second, variables and expressions

Variable initialization: This variable is initialized while defining variables.

INT width = 10; // Define and initialize variables

Long line splitting: long expressions should be split into new rows at a low priority operator, and operators are in the first of the new row to stand out

Operators.

Modifier: The modifier * and & to close the variable name.

Char * name;

INT * x, y; // This Y is not mistaken to be a pointer

Type of class: Before the behavior, the member function is before.

Naming Rules: 1. Member function of class with verbs Box-> DRAW ();

2. Global function with verbs object drawbox ();

Operation priority: Determine the order of operation with brackets.

IF ((A | B) && (A & C))

Composite expression: Do not confuse with real mathematical expressions.

IF (a

Not expressed

IF ((a

Indicates IF (((a

转载请注明原文地址:https://www.9cbs.com/read-7542.html

New Post(0)