Header file
Each C++/C program is typically divided into two files. A file is used to save a program's declaration (declaration), called a header file. Another file is used to save the implementation of the Program (Implementation), known as the definition file.
The C++/C program's header file is suffix ". h", and the C program's definition file is suffix ". C", and the C + + program's definition file is usually suffix ". cpp" (There are also systems with ". CC" or ". Cxx" as suffixes).
1.1 Statement of copyright and version
The copyright and version declarations are at the beginning of the header file and the definition file (see example 1-1), and the main contents are:
(1) Copyright information.
(2) file name, identifier, abstract.
(3) Current version number, author/modifier, finish date.
(4) Version history information.
/*
* Copyright (c) 2001, Old Demon Studio
* All rights reserved.
*
* File name: filename.h
* File identification: See Configuration management Plan
* Summary: Brief description of the contents of this document
*
* Current Version: 1.1
* Author: Enter the name of the author (or modifier)
* Date of Completion: July 20, 2001
*
* Replacement Version: 1.0
* Original Author: Enter the original author (or modifier) name
* Date of Completion: May 10, 2001
*/
Example 1-1 Statement of copyright and version
1.2 Structure of the header file
The header file consists of three parts:
(1) The Copyright and version declaration at the beginning of the header file (see Example 1-1).
(2) pretreatment block.
(3) Function and class structure declaration, etc.
Assuming that the header file name is called Graphics.h, the header file structure is shown in example 1-2.
Rule 1-2-1 to prevent header files from being repeatedly referenced, a preprocessing block should be generated using the IFNDEF/DEFINE/ENDIF structure.
Rule 1-2-2 uses the #include <filename.h> format to refer to the header file of the standard library (the compiler will start searching from the standard library directory).
Rule 1-2-3 uses the #include "filename.h" format to refer to a header file for a nonstandard library (the compiler will start searching from the user's working directory).
The recommendation 1-2-1 header file only holds "declaration" and does not contain "definition"
In C + + syntax, a member function of a class can be defined at the same time as a declaration and automatically become an inline function. Although this will bring the convenience of writing, but it caused the style inconsistency, more harm than good. It is recommended that you separate the definition of a member function from the declaration, regardless of how small the function body is.
"Recommendation 1-2-2" does not advocate the use of global variables, as far as possible not to appear in the header file like extern int value such declarations.
Copyright and version Statement See example 1-1, omitted here.
#ifndef Graphics_h//Prevent Graphics.h from being repeatedly referenced
#define Graphics_h
#include <math.h>//reference header file for standard library
...
#include "myheader.h"//reference to a header file of a nonstandard library
...
void Function1 (...); global function declaration
...
Class Box//struct declaration
{
...
};
#endif
Example 1-2 the structure of a c++/c header file
1.3 Structure of the definition 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) A reference to some header file.
(3) The implementation body of the program (including data and code).
Suppose the name of the definition file is graphics.cpp, and the structure of the definition file is shown in example 1-3.
Copyright and version Statement See example 1-1, omitted here.
#include "graphics.h"//reference header file
...
The implementation body of global function
void Function1 (...)
{
...
}
Implementation body of class member function
void Box::D raw (...)
{
...
}
Example 1-3 C++/C defines the structure of a file
1.4 Function of the header file
Early programming languages such as BASIC, Fortran no header file concept, c++/c language beginners, although using the header file, but often unknown. Here is a brief explanation of the function of the right document:
(1) Call library function through header file. In many cases, the source code is inconvenient (or not allowed) to the user, as long as the user to provide a header file and binary library can be. The user simply needs to invoke the library functionality according to the interface declaration in the header file, without having to worry about how the interface is implemented. The compiler extracts the appropriate code from the library.
(2) header file can enhance type safety check. If an interface is implemented or used in a way that is inconsistent with the declaration in the header file, the compiler will point out the error, a simple rule that can greatly reduce the burden of debugging and error-changing for programmers.