Table of contents Preface 6 Chapter 1 File Structure 11 1.1 Copyright and version statement 11 1.2 Structure of header file 12 1.3 Definition file structure 13 1.4 The role of header files 13 1.5 Directory structure 14 Chapter 2 Program Layout 15 2.1 Blank line 15 2.2 Line of code 16 2.3 Spaces within lines of code17 2.4 Alignment18 2.5 Long line splitting 19 2.6 Position of modifiers 19 2.7 Note 20 2.8 Class layout 21 Chapter 3 Naming Rules 22 3.1 Common rules 22 3.2 Simple WINDOWS application naming rules 23 3.3 Simple UNIX application naming rules 25 Chapter 4 Expressions and Basic Statements 26 4.1 Operator precedence 26 4.2 Compound expressions 27 4.3 IF statement 27 4.4 Efficiency of loop statements 29 4.5 Loop control variables of FOR statement 30 4.6 SWITCH statement 30 4.7 GOTO statement 31 Chapter 5 Constants 33 5.1 Why constants are needed 33 5.2 CONST vs. #DEFINE 33 5.3 Rules for defining constants 33 5.4 Constants in classes 34 Chapter 6 Function Design 36 6.1 Rules for parameters 36 6.2 Rules for return values 37 6.3 Rules for internal implementation of functions 39 6.4 Other suggestions 40 6.5 Using assertions 41 6.6 Comparison of references and pointers 42 Chapter 7 Memory Management 44 7.1 Memory allocation method 44 7.2 Common memory errors and their countermeasures 44 7.3 Comparison of pointers and arrays 45 7.4 How do pointer parameters transfer memory? 47 7.5 What happens to pointers with FREE and DELETE? 50 7.6 Will dynamic memory be released automatically? 50 7.7 Eliminate “wild pointers” 51 7.8 Why do we need NEW/DELETE when we have MALLOC/FREE? 52 7.9 What should I do if the memory is exhausted? 53 7.10 Key points for using MALLOC/FREE 54 7.11 Key points for using NEW/DELETE 55 7.12 Some insights 56 Chapter 8 Advanced Features of C++ Functions 57 8.1 The concept of function overloading 57 8.2 Overloading, overriding and hiding of member functions 60 8.3 Default values of parameters 63 8.4 Operator overloading 64 8.5 Function inlining 65 8.6 Some insights 68 Chapter 9 Constructors, Destructors and Assignment Functions of Classes 69 9.1 The Origin of Constructors and Destructors 69 9.2 Initialization table of constructor 70 9.3 Order of construction and destruction 72 9.4 Example: Constructor and destructor of class STRING 72 9.5 Don’t underestimate copy constructors and assignment functions 73 9.6 Example: copy constructor and assignment function of class STRING 73 9.7 The lazy way to deal with copy constructors and assignment functions 75 9.8 How to implement the basic functions of a class in a derived class 75 9.9 Some insights 77 Chapter 10 Class Inheritance and Composition 78 10.1 Inheritance 78 10.2 Combination 80 Chapter 11 Other Programming Experiences 82 11.1 Using CONST to improve function robustness 82 11.2 Improving program efficiency 84 11.3 Some helpful suggestions 85 Reference 87 Appendix A: C++/C Code Review Form 88 Appendix B: C++/C test questions 93 Appendix C: Answers and scoring criteria for C++/C test questions 97