C + + Programming for high quality (1) __ Programming

Source: Internet
Author: User
Tags modifier
Chapter 1th Document Structure 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, Shanghai Bell Co., Ltd. Network application Division * All rights reserved. * * File name: filename.h * File ID: See Configuration Management Plan * Summary: Brief description of the contents of this document * * Current version: 1.1 * Author: Enter the author (or modifier) name * Date of Completion: July 20, 2001 * Replace version Ben: 1.0 * Original Author: Enter original author (or modifier) name * Completion Date: 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. L"Rule 1-2-1 "To prevent header files from being repeatedly referenced, a preprocessing block should be generated using the IFNDEF/DEFINE/ENDIF structure. L"Rule 1-2-2 "Use 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). L"Rule 1-2-3 "Use 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). ²"Suggested 1-2-1 "The header file only holds "declaration" and does not hold "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. ²"Suggested 1-2-2 "Use of global variables is not advocated, and a declaration such as extern int value should not appear in header files.
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 "m    Yheader.h "//reference header files for non-standard libraries ... 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 ...//global function Implementation body void Function1 (...)   { ... } Implementation of class member functions 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 file: (1) Call the library function through the 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. 1.5 directory structure If a software has a larger number of header files (for example, more than 10), you should usually save the header and definition files in separate directories for easy maintenance. For example, you can save a header file in the Include directory and save the definition file in the source directory (which can be a multilevel directory). If some header files are private and are not referenced directly by the user's program, there is no need to expose their "declarations." To enhance information hiding, these private header files can be stored in the same directory as the definition files.   &NBSP;

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.