High quality C ++ & C Programming Guide
This ebook was provided by instructors in courseware resources during the course of the National Science University. The reason why I pay attention to this document is that when I was a undergraduate, a teacher mentioned that the student's code is neat and looks comfortable, so I knew that I could not keep him. Therefore, I pay special attention to this issue, but I did not carefully search for relevant information. Now I have the opportunity to find it. Of course I will save it.
There are already three cycles for learning C ++. The first cycle is simple MFC processing, and there is no complete result output, it took me two weeks to learn some routines.
In the second cycle, I did not learn any specific methods, but learned more about objects and classes. Objects and classes are like the relationship between moon cakes and templates.Why is there a faster object? Just like building a building, it is better to use bricks than reinforced concrete, and the existence of classes is the "Reinforced Concrete", which is a relatively larger block.
The third cycle is to really learn the specific content of C ++ and master the basic syntax. During this period, I once thought about plagiarism, but I still bit my teeth to write a simple program and try to make it "regular". So now I have a very specific understanding of constructors, virtual functions, and so on, quick Start.
Study Notes
What you don't know
- If an interface is implemented or used in a different way than the declaration in the header file, the compiler will point out an error. This simple rule can greatly reduce the burden on programmers to debug and correct errors.
New Content
Directory structure:
You can save the header file to the include directory and the definition file to the source directory (which can be a multi-level directory)
# Include <math. h> // reference the header file of the standard library # include "myheader. h" // reference the header file of the non-standard library
Variable assignment:
Assign values when defining variables as much as possible to prevent future forgetting.
Space:
For long for statements and if statements, some spaces can be removed for the sake of conciseness, such
For (I = 0; I <10; I ++) And if (a <= B) & (c <= d ))
I used to do not know how to remove spaces. It takes a long time to use.
Writing pointer variables:
Int * x, y; // here y will not be misunderstood as a pointer
Note:
[Rule 2-7-6] The comments should be located adjacent to the described code. They can be placed at the top or right of the Code and not at the bottom.
[Rules 2-7-8] When the code is relatively long, especially when there are multiple nesting rules, comments should be added at the end of some paragraphs to facilitate reading.
Class declaration:
For class declaration, first write the public function so that you can see:
I suggest that you use the "behavior-centered" writing method, that is, first consider what functions the class should provide. This is the experience of many people-"This not only makes your mind clearer during design, but also makes it easier for others to read. Because users are most concerned about interfaces, who are willing to see a bunch of private data members first !"
Naming rules:
Naming rules are new to me. I used to wander between windows program and unix program naming rules, and now I have a clear solution:
Single-character names are also useful. Common ones include I, j, k, m, n, x, y, and z. They are usually used as local variables in functions.
[Rule 3-1-7] The name of a global function should be "verb" or "Verb + noun ).
Class member functions should only use the "verb", and the omitted nouns are the object itself.
For example:
DrawBox (); // global function box-> Draw (); // a member function of the class
[3-1-1] avoid numbers in the name, such as Value1 and Value2, unless a number is required logically. This is to prevent programmers from being lazy and refuse to name their brains, resulting in meaningless names (because numbers are the most convenient ).
Rule 3-2-1: The class name and function name are combined with words starting with an upper-case letter.
Rule 3-2-2: a combination of variables and parameters starting with a lowercase letter.
[Rule 3-2-3] constants are all separated by uppercase letters and underscores.
[Rule 3-2-4] static variables are prefixed with s _ (indicating static ).
[Rule 3-2-5] If you have to require a global variable, add the global variable with the prefix g _ (indicating global ).
Rules 3-2-6: The data member of the class is prefixed with m_( indicating member). This prevents the data member from having the same name as the parameter of the member function.
[Rules 3-2-7] to prevent conflicts between some identifiers in a software library and other software libraries, you can add prefixes that reflect the nature of software for various identifiers. For example, all library functions of 3D graphics standard OpenGL start with gl, and all constants (or macro definitions) start with GL.
If statement:
- Boolean variables cannot be compared with zero;
- Integer variables can be compared with zero, but cannot be compared with "! A "form;
- Do not use "=" or "! = "Compare with any number;
- Use "=" or "! = "For comparison with NULL, although NULL = 0, pointer variables should be distinguished;
if (condition) return x;return y;
Is available, but not standardized, and can be changed to a more concise expression:
return (condition ? x : y);
Efficiency of loop statements:
[4-4-1] if possible, the longest cycle should be placed in the innermost layer, and the shortest cycle should be placed in the outermost layer to reduce the number of times the CPU switches across the cycle layer.
[4-4-2] If there is a logical judgment in the loop body and the number of cycles is large, it is recommended to move the logical judgment outside the loop body.
First five chapters
Study time: 2PT
Sorting time: 1PT
March 7, 2018 16:01:00