C + + Learning Tour Second, C + + header file _c language

Source: Internet
Author: User
First, C + + header file What exactly is, how do you think?

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. C++/C Program header file with ". h" as the suffix, c program definition file with ". C" as the suffix, C + + program definition files are usually suffix ". cpp" (such as Linux, such as the ". CC" or ". cxx" suffix). In short, is the file for the declaration.

As a. NET programmer, we will ask this question, the header file in C #, what do you think?

C # is declared and defined in one place, equivalent to putting the head file in the execution file. When you use a generic reference, you only need to introduce the namespace with using the. As we. NET programmers can understand, header files are defined as namespaces, which, when introduced, is equivalent to introducing namespaces. We are introducing namespaces directly in. Net.

second, what is the content of C + + header file, what do you think?

The header file consists of three parts:

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

This part is structured as follows:

① copyright information. ② file name, identifier, summary. ③ Current version number, author/modifier, finish date. ④ version history information.

We have no such structure in. NET, what do you think? Yes, AssemblyInfo.cs is not a detailed record of this information.

(2) pretreatment block.

Take a look at the pretreatment block. As a. NET programmer, we are not unheard of for preprocessing blocks. His meaning is very simple, before compiling the processing. There is a preprocessor in the C + + compiler, and we do not have a single preprocessing concept in. Net. He has been divided into 3 stages:

2. Document contains; 3. Conditional compilation.

What is a macro definition,

1. Macro definition with no parameters:

A macro definition is also called a macro substitution, a macro substitution, or a macro. Format: #define identifier string where the identifier is called a symbolic constant, also known as the "macro name." Preprocessing (precompiled) work is also called macro expansion: Replace the macro name with a string. The key to mastering the concept of "macro" is "change". Everything to change for the premise, do anything before you want to change, accurate understanding before you must "change."

a macro with no parameters is described as follows:

(1) Macro names are generally capitalized; (2) The use of macros can improve the versatility and readability of the program, reduce inconsistencies, reduce input errors and facilitate modification. For example: array size common macro definition, (3) preprocessing is the processing before compiling, and one of the tasks of compiling work is grammar check, preprocessing does not do grammar checking. (4) A macro definition is not preceded by a semicolon, (5) The macro definition is written outside the curly braces of the function, and the scope is followed by the program, usually at the very beginning of the file. (6) You can use the #undef command to terminate a macro-defined scope (7) macro definition can be nested (8) string "" Never contain macros (9) macro definition does not allocate memory, variable definition allocates memory.

2. Macros with parameters:

In addition to the general string substitution, but also to do parameter substitution format: #define Macro name (parameter table) string

Description of macros with parameters:
(1) Arguments are problematic if they are expressions
#define S (R) r*r
Area=s (a+b); The first step is Area=r*r, and the second step is area=a+b*a+b;
The correct macro definition is #define S (r) * (R);
(2) There should be no spaces between the brackets between the macro name and the parameters;
(3) The macro substitution is only substituted, does not do the computation, does not do the expression solution;
(4) function calls are performed when the compiled program is run, and memory is allocated. Macro substitution is done before compiling, and does not allocate memory;
(5) The dummy combination of macros does not exist a type, and there is no type conversion.
(6) The function has only one return value, the use of the macro can be managed to get multiple values;
(7) Macro expansion makes the source program become longer, and function calls are not;
(8) Macro expansion does not account for running time, only for compile time, function call to run time (allocate memory, keep field, Value Pass, return value)
Said so many C + + macro definition, we. NET is not supporting macros, the answer is yes. You can use the above define keyword to implement the C + + macro definition process, typically in the header of a class declaration.

Two file contains--one file contains the contents of another file

Format:
#include "filename" or #include < filename >
The included files are part of the source file at compile time to include the processed files as compilation units. Only one target file has been compiled. obj, the included file is also referred to as "header file" or "head file", "header File", and the. h is used as an extension. After you modify the header file, all files that contain the file are recompiled. In addition to the function prototype and macro definition, the contents of the header file can be defined by the structure, and the global variable definition: (1) A #include command specifies a header file, (2) file 1 contains file 2, file 2 is used for file 3, The containing command for file 3 should be placed on the first line of the head of file 1, (3) contains can be nested, (4) < filename > is called the standard way, the system header file directory looks for files, "file name" is first in the current directory lookup, and then the header file directory search #include (5) Static global variables in the included file are not declared in the include file.

Said here, we. NET is also a document contains the concept of how No, you using the namespace, is not the best. net file contains embodied. He compiles the. obj file as well.

Three conditional compilation--some statement lines want to compile when the condition is satisfied.

Format: (1) #ifdef identifier program segment 1#else program segment 2#ENDIF or #ifdef program Segment 1 #endif当标识符已经定义时, program paragraph 1 to participate in the compilation. Format: (2) #ifndef identifier format: (3) #if expression 1 Program section 1 #else程序段2 #endif
When expression 1 is established, compile the program section 1, when not immediately, compile the program section 2. Use conditional compilation to make the target program smaller and run less time. Precompiling to increase the number of solutions to problems or algorithms helps us choose the right solution. In addition, there is the layout control: #progma, which is one of the important aspects of our application preprocessing, the main function is to provide the compiler with unconventional control flow information
In. Net there are also his conditions compiled, but also a bit of processing format:
#if #elif #else #endif;

However, we usually rarely use the project, to learn his specific blog for details:
Http://www.jb51.net/article/31701.htm

(3) Function and class structure declaration, etc.

As for the declarations of functions and class structures, there is really no need to repeat them. You understand that as an interface declaration, it's OK to implement it in the source file.

The header file says so much and also does the. NET comparison. We conclude with a word. NET actually has the header file, but is scattered in each individual file, but C + + put these content together.

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.