Chapter 1 File Structure
Each C / C program is usually divided into two files. A file is used to save the program's declaration, called a header file. Another file is used to save the implementation of the program, called the definition file.
The header of the C / C program is ".h" as a suffix, the definition file of the C program is ".c" as a suffix, the definition file of the C program is usually ".cpp" as a suffix (there are also some systems ".cc" Or ".cxx" is a suffix).
1.1 Copyright and version declaration
Copyright and version of the statement is located at the beginning of the header file and the definition file (see Example 1-1), the main contents are:
(1) Copyright information.
(2) File name, identifier, summary.
(3) Current version number, author / modifier, complete date.
(4) Version history information.
/ *
* CopyRight (C) 2001, Shanghai Bell Co., Ltd. Network Application Division
* All Rights Reserved.
*
* File Name: filename.h
* File ID: See Configuration Management Program
* Summary: Summary describes the contents of this document
*
* Current version: 1.1
* Author: Enter the author (or modifier) name
* Completion date: July 20, 2001
*
* Replacement version: 1.0 * Original Author: Enter the original author (or modifier) name
* Completion date: May 10, 2001
* /
Example 1-1 Copyright and Version Declaration
1.2 structure of the header file
The header file consists of three parts:
(1) Copyright and version declaration at the beginning of the header (see Example 1-1).
(2) Preparation block.
(3) Functions and class structure declarations.
Assume that the header file name is graphics.h, the structure of the header file is shown in Example 1-2.
l [Rules 1-2-1] In order to prevent the header files from being repeatedly referenced, the preparation block should be generated using the IFNDEF / DEFINE / ENDIF structure.
l [Rule 1-2-2] Use a #include
l [Rules 1-2-3] Use the #include "filename.h" format to reference the unbalanced base file (the compiler will start searching from the user's work directory).
2 [Recommendation 1-2-1] only stores "declaration" in the header file without storing "definition"
In the C syntax, the members of the class can be defined while the declaration is defined and automatically becomes inline functions. Although this will bring the convenience of writing, but it has caused the style inconsistency, and the disadvantages are greater than the profit. It is recommended to separate the definitions and statements of the member function, regardless of how small the function is.
2 [Recommendation 1-2-2] Do not advocate global variables, try not to appear in the header files in the header files Extern Int value.
// Copyright and version declaration See Example 1-1, omitted here.
#ifndef graphics_h / / Prevent Graphics.h from being repeatedly referenced
#define graphics_h
#include
... #include "myHeader.h" // Reference Non-standard library header file ...
Void function1 (...); // global function declaration
...
Class box // class structure declaration
{
...
}
#ENDIF
Example 1-2 Structure of C / C header file
1.3 Defining the structure of the file
The definition file has three parts:
(1) Define the copyright and version declaration at the beginning of the file (see Example 1-1).
(2) Quote for some headers.
(3) The implementation of the program (including data and code).
Assuming that the name of the definition file is graphics.cpp, the structure of the definition file is defined in Example 1-3.
// Copyright and version declaration See Example 1-1, omitted here.
#include "graphics.h" // Reference header file
...
// implementation of global functions
Void function1 (...)
{
...
}
Property of // class member function
Void Box :: Draw (...)
{
...
}
Example 1-3 C / C definition file structure
1.4 head file role
Early programming language such as Basic, Fortran does not have a header of the header, although the initiator of the C / C language will use the header file, but often unclear. The role of the header file here is slightly explained:
(1) Call the library function through the header file. In many cases, the source code is inconvenient (or not allowed) to publish to the user, as long as the header file and the binary library are available to the user. Users only need to call the library function according to the interface declaration in the header file without having to care about how the interface is implemented. The compiler will extract the corresponding code from the library.
(2) Header file enhances type safety inspection. If an interface is implemented or used, its way is inconsistent with the statement in the header file, the compiler will point out errors, this simple rule can greatly reduce programmers debugging, and the burden of changing the wrong.
1.5 directory structure
If a software's header file is more (such as more than ten), the header file and definition file should usually be saved in different directories for maintenance.
For example, the header file can be saved in the include directory, and the definition file is saved in the Source directory (which can be a multi-level directory).
If some headers are private, it is not directly referenced by the user, and it is not necessary to disclose its "declaration". In order to enhance information hidden, these private headers can be stored in the same directory and definition files.