High quality c ++ Programming Guide Chapter 1-2-thanks to Lin Rui

Source: Internet
Author: User

Reading this book mainly aims to regulate your programming habits, so reading this book is just the beginning and will be used in practice in the future. Great Expectations.

Introduction: Chapter 1-6 is described in the Guide, which is difficult and detailed. Chapter 7-11 is a special topic discussion with more thoughts. Let's take a look at the first six chapters. Then perform the test on the appendix. I don't know if it will be too exaggerated to read the chapter one day from 7 to 11.

Book Planning Adjustment version 1: Read Chapter 2 today, tomorrow 3-4, the day after tomorrow 5-6.

Book Planning Adjustment version 2: the first six chapters are reviewed to Monday.

Book planning adjusted version 3: 2 on the 22nd, and travel on the 25th.

Chapter 1
File structure

Each C ++/C program is generally divided into two files. A file is the header file used to save the program declaration. Another file is used to save the implementation of the program, called the definition file.

1. 1 copyright and version statement

It is located at the beginning of the header file and definition file. The main contents include:

1)
Copyright information.

2)
File Name, identifier, and summary.

3)
Current version number, author/modifier, completion date.

4)
Version history.

Example 1-1:

/*

* Copyright (c) 2001, Network Application Department of Shanghai Bell Co., Ltd.

* All Rights Reserved.

*

* File name: Filename. h

* File ID: see the configuration management plan.

* Abstract: Briefly describe the content of this document.

*

* Version: 1.1

* Author: Enter the author (or modifier) Name.

* Completion date: January 1, July 20, 2001

*

* Replace: 1.0

* Original Author: Enter the original author (or modifier) Name.

* Completion date: January 1, May 10, 2001

*/

Header file structure

The header file consists of three parts:

1) Copyright and version statement at the beginning of the header file.

2) pre-processing block.

3) function and class structure declaration.

Follow the following rules and suggestions:

[Rule 1-2-1] to prevent header files from being repeatedly referenced, pre-processed blocks should be generated using the ifndef/define/endif structure.

[Rule 1-2-2] Use # include <> to reference the standard library header file and # include "" to reference the non-standard library header file.

[1-2-1] the header file only contains the Declaration and does not store the definition.

[1-2-2] global variables are not recommended. do not include declarations such as extern int value in header files.

Assume that the header file name is called graphics. h, and the header file structure Example 1-2:

// For version and copyright notice, see Example 1-1, which is omitted here

# Ifndef graphics_h // prevent graphics. h from being repeatedly referenced

# Define graphics_h

# Include <maht. h> // reference the header file of the standard library

# Include "myheader. h" // reference the header file of a non-standard library

Void function1 (); // global function declaration

Class box // class structure Declaration

{

}

# Endif

1. 3 define the file structure

The definition file contains three parts:

1)
Define the copyright and version statement at the beginning of the file (see example 1-1 ).

2)
References some header files.

3)
Program Implementation Body.

Assume that the definition file is named graphics. cpp. for the structure of the definition file, see Example 1-3:

// For version and copyright notice, see Example 1-1, which is omitted here

# Include "graphics. cpp" // reference the header file

// Global function implementation body

Void function1 (...)

{

......

}

 

// Implementation body of class member functions

Void box: Draw (...)

{

......

}

1. 4. Functions of header files

1) Use the header file to call the library function. In many cases, source code cannot be published. Only header files and binary libraries are provided. You only need to follow the interface declaration in the header file to call the library function.

2) the header file can enhance the type security check. If an interface is implemented or used in a different way than the declaration in the header file, the compiler will indicate an error.

1. 5 directory structure

If the number of header files is large (for example, more than 10 files), the header files and definition files should be stored in different directories for maintenance. For example, you can save the header file to the include directory and the definition file to the source directory (but multi-level Directories ).

If some header files are private and are not directly referenced by the user program, it is not necessary to disclose their "Declaration ". To enhance information hiding, these private header files and definition files can be stored in the same directory.

 

Summary: This chapter describes the file structure and first understands what the file structure is. The first section shows that the file structure consists of a header file and a definition file. Next, we will explain the header file and definition file. The similarities between header files and definition files are discussed by the author separately. And then explain them separately. The role of header files is described separately, mainly because many languages did not have the concept of header files at that time. Finally, I talked about the directory structure. I should have learned it from practice.

Chapter 2 Program Layout

The layout affects readability. The layout of the program is clear and beautiful.

2. 1 blank line

Empty rows separate program paragraphs.

[Rule 2-1-1] Empty rows must be added after each class declaration and after each function definition.

[Rule 2-1-2] In a function, logically closely related statements are not added with blank lines, but are added elsewhere.

2. 2. Code lines

[Rule 2-2-1] a line of code only does one thing, for example, defining only one variable or writing only one statement.

[Rule 2-2-2] If, for, while, do and other statements each occupy one row, and the execution statement cannot be followed. {} Must be added no matter how many statements are executed {}.

[2-2-1] Try to initialize the variable while defining the variable (proximity principle ).

2. 3. spaces in the code line

[Rule 2-3-1] with a space after the keyword. For example, if, for, and while are followed by a space and '('.

[Rule 2-3-2] Do not leave a space after the function name, followed by the left brace '(', which is different from the keyword.

[Rules 2-3-3] '('backward followed,') ','; 'follow forward, followed by no spaces.

[Rule 2-3-4] ',' followed by a space, such as function (x, y, z ). If ';' is not the end symbol of a row, leave a space after it, such as for (initializition;
Condition; update ).

[Rule 2-3-5] spaces should be added before and after the value assignment operator, comparison operator, arithmetic operator, bare metal operator, bit operator and other binary operators.

[Rule 2-3-6] The unary operator is like "!" ,"~" , "", "+ +", "&", And so on.

Rules 2-3-7 do not contain spaces before operators such as "[]", ".", and "->.

[2-3-1] For the for statement and if statement with long expressions, remove some spaces for compact purposes. For example, for (INT I = 0;
I <10; I ++) And if (A <= B) & (c <= D )).

2, 4 alignment

[Rule 2-4-1] the program's delimiters '{' and '}' Should exclusive a row in the same column and be left aligned with the statements that reference them.

The code block in [Rule 2-4-1] {} is left aligned at the right digit.

2. Split 5 long rows

[Rule 2-5-1] The maximum generation of code lines should be less than 70-80 characters.

[Rule 2-5-2] A long expression must be split into new rows at the lower-priority operator, and the operator must be placed at the beginning of the new line (to highlight the operator ). The new lines to be split should be properly indented to make the layout neat and the statement readable.

2. 6 modifier location

[Rule 2-6-1] the modifier * and & should be placed close to the variable name. For example, if int * X, Y; // If x and y do not want to be defined by a branch, y will not be misunderstood as a pointer.

2. 7 annotations

[Rule 2-7-1] annotations are "prompts" for codes, rather than documents. They should be accurate and easy to understand.

[Rule 2-7-2]: write code and comment, modify the code, and modify the corresponding comment.

[Rule 2-7-3] The comments should be located adjacent to the described code, which can be placed at the top or right of the Code.

[Rule 2-7-4] When the code is long, especially when there are multiple nesting rules, comments should be added at the end of some paragraphs.

2. 8 types of Layout

There are two main types of layout:

1) write the private type data to the front, and write the public type function to the back. The design of this layout claim class is "Data bit Center" and focuses on the internal structure of the category.

2) write the public type function before the signature, and write the private type data after the signature. The design of this type of layout proposition class is "behavior-centered", and focuses on what interfaces should be provided by the category class. We recommend that you use this method-not only to make your ideas clearer during design, but also to make it easier for others to read.

Summary: how to make a good presentation of the program layout? How can this problem be solved? The author defines: clear and beautiful. How is it clear and beautiful. First, the style should be unified, and secondly the beauty. Then we will discuss the style first. The style is not the code format. The code format must know the type of the Code. It is probably a variable, function, class, keyword, space, and empty line. Don't forget to comment out. At first, I 've been struggling to write the code above or below. And.

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.