Structure of the 3-c++ program 1.5

Source: Internet
Author: User
Tags function prototype

Multi-file structure and compilation preprocessing commands

1. General organizational structure of C + + programs

Typically, a project is divided into at least three files: a class definition file (*.h file), a class implementation file (*.cpp file), and a class's usage file (*.cpp, the main function file). As follows:

These three separate source files are composed of their interrelationship and compilation, and the connection process is as follows

2. External variables and external functions

A, in order for the variable to be used in addition to the file in which it is defined, it is also used by other files and can be declared as an external variable, using the extern keyword.

An external variable has a file-scoped variable that is defined outside of all files. When declaring an external variable, you can define it at the same time, or you can simply refer to an external variable declared elsewhere.

When you define a variable for a file scope, its default state is that it can be shared for a different compilation unit (that is, the source file), as long as the variable is declared in the other compilation unit with extern.

b, external functions

Functions declared outside of all classes (non-member functions) have a file scope, and if there are no special instructions, such functions can be called in different compilation units, as long as the function prototype is called before the call.

3. Standard C + + libraries and namespaces

In the C language, system functions and some macro definitions are placed in the runtime, and in addition to the C system functions in the C + + library, the predefined templates and classes are added. The standard C + + class library is a highly flexible and extensible collection of reusable software templates. The standard C + + classes and components are logically divided into 6 types:

Input/Output Classes

Container classes and ADT (abstract data types)

Storage Management Classes

Algorithm

Error handling

Operating Environment support

The description of the predefined content in the library exists in different header files, and to use these predefined components, the corresponding header file is included in the Circle program. Different compilation systems, libraries and header files are slightly different.

4. Compilation preprocessing

Before the compiler compiles the source program, the program text is first preprocessed by the preprocessor. The preprocessor provides a set of compilation preprocessing directives and preprocessing operators.

A, #include指令

Also known as a file-containing directive, its purpose is to embed another source file at that point in the current source file. Available in two formats: #include < file name > and # include "File name"

B, #define and #undef directives

In C, you can also use # define to define a parametric macro to simply implement the calculation of a function, which has been superseded by inline functions in C + +.

#undef的作用是删除 the macro defined by # define so that it no longer works.

C, conditional compilation

Form 1

#if constant expression//when the constant expression is nonzero, compile this program segment

Program Segment

#endif

Form 2

#if constant expression

Procedure Section 1

#else

Procedure Section 2

#endif

Form 3

#if constant Expression 1

Procedure Section 1

#elif constant Expression 2

Procedure Section 2

....

#elif constant-expression n

Program Segment N

#else

Program Segment N+1

#endif

Form 4

#ifdef identifiers

Procedure Section 1

#else

Procedure Section 2

#endif

If the identifier is defined by a # define and is not undef deleted, then the program Segment 1 is compiled, otherwise, the program segment 2

Form 5

#ifndef identifiers

Procedure Section 1

#else

Procedure Section 2

#endif

In contrast to form 4

4. Defined operator

It is a preprocessing operator, not an instruction, so do not add # Start, the defined operator is used in the form of: defined (identifier)

If the identifier was previously defined by a # define and is not deleted, the expression above is not 0. Otherwise the value of the above expression is 0.

Because the file contains directives can be used in nested, the design program is to avoid repeating the same header file multiple times, otherwise it will cause a duplicate definition of variables and classes.

It is usually added in the header file

Head.h

#ifndef Head_h

#define Head_h

.......

First determine whether Head_h is defined, not the first compile, if defined, it is already participated in the compilation.

Structure of the 3-c++ program 1.5

Related Article

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.