C++ learning journey ii says about C++ header files

  • 2020-04-01 21:26:37
  • OfStack

What is the C++ header file? What do you think?

Each C++/C program is typically divided into two files. A file is used to save a program's declaration, called a header file. Another file is used to save the implementation of the program, called a definition file. C++/C programs' header files are suffixed with ".h ", C programs' definition files are suffixed with ".c ", and C++ programs' definition files are usually suffixed with ".cpp "(for systems like Linux,".cc "or".cxx "). In short, it is the file for the declaration.

So we, as.net programmers, we ask the question, what do you think about header files in C#?

Declaration and definition in c# in one place, equivalent to the head file in the executable. For general references, you simply use using to introduce a namespace. For us.net programmers, the header file is the equivalent of the definition of a namespace, which, when introduced, is the equivalent of the introduction of a namespace. We introduced namespaces directly into.net.

What do you think of C++ header files?

The header file consists of three parts:

(1) copyright and version notice at the beginning of header file

This part has the following structure:

Copyright information. (2) file name, identifier, summary. (3) current version number, author/modifier, completion date. (4) version history information.

Do we have a similar structure in.net? What do you think? Assemblyinfo.cs is simply a collection of information.

(2) pretreatment block.

Take a look at the preprocessor block. For those of us who are.net programmers, you're not unheard of for preprocessing blocks. What it means is very simple, processing before compiling. There is a preprocessor in the C++ compiler, and we don't have a separate preprocessor concept in.net. He had three stages:

2. File include; 3. Conditional compilation.

What is a macro definition,

1. Macro definition without parameters:

Macro definition is also known as macro substitution, macro replacement, referred to as "macro". The identifier in the #define identifier string is called a symbolic constant, also known as a "macro name." Preprocessing (precompilation) is also called macro expansion: replacing the macro name with a string. The key to mastering the concept of macros is substitution. Everything with the premise of change, do anything before you have to change, accurate understanding of the need to "change".

The description for macros without parameters is as follows:

(1) macro names are generally capitalized; (2) macros can improve the universality and readability of the program, reduce inconsistencies, reduce input errors and facilitate modification. For example, array sizes are commonly defined by macros; (3) preprocessing is the processing before compilation, and one of the tasks of compilation is syntax checking, preprocessing does not do syntax checking. ; (4) there is no semicolon at the end of the macro definition; (5) the macro definition is written outside the curly braces of the function and is scoped to the program that follows, usually at the beginning of the file. (6) the scope of the macro definition can be terminated with the #undef command (7) macro definition can be nested (8) string "" never contain macro (9) macro definition do not allocate memory, variable definition allocate memory.

2. Macro with parameters:

In addition to the normal string substitution, do the parameter substitution format: #define macro name (parameter table) string

Description of macro with parameters:
(1) arguments are prone to problems if they are expressions
# define S (r) (r * r
Area = S (a + b); The first step is area=r*r; , the second step is changed to area=a+b*a+b;
The correct macro definition is #define S(r) (r)*(r);
(2) there should be no space between the brackets of macro name and parameter;
(3) macro substitution only makes substitution, does not do the calculation, does not do the expression solution;
(4) the function call is made when the program is run after compilation and memory is allocated. Macro substitution is performed before compilation and memory is not allocated.
(5) there is no type in the matte combination of macros, and there is no type conversion.
(6) the function only has a return value, using macros can try to get more than one value;
(7) macro expansion makes the source program longer, the function call will not;
(8) macro expansion does not take up the running time, only the compilation time, the function call takes up the running time (allocate memory, preserve the scene, value transfer, return value)
Say so many C++ macro definition, we.net is not support macros, the answer is yes. The above define keyword can be used to implement the C++ macro definition process, which is generally declared in the head of the class.

File contain - one file contains the contents of another file

Format:
#include "file name" or #include < The file name >
The compilation unit is the included file, which is part of the source file. After compiling, only one object file is obtained. Obj, the included file is also called "title file" or "header file", "header file", and is often used as an extension. H. After modifying the header file, all files containing the file should be recompiled. In addition to function prototype and macro definition, the contents of the header file can also have structure definition, global variable definition :(1) a #include command to specify a header file; (2) file 1 contains file 2, file 2 USES file 3, so the include command #include of file 3 should be placed on the first line of the header of file 1; (3) contains can be nested; (4) < The file name > Called the standard way, the system head file directory to find the file, "file name" is first in the current directory to find, and then head file directory to find; (5) the static global variables in the included file need not be declared in the included file.

Say here, we.net is not also the concept of file included, how did not, you use namespace, is not the best. He also compiles.obj files.

Conditional compilation - some statement lines want to be compiled only when the condition is satisfied.

Format :(1) #ifdef identifier segment 1#else segment 2#endif or #ifdef segment 1# endif does not participate in compilation until the identifier has been defined. Format :(2) #ifndef identifier format :(3) #if expression 1 segment 1#else segment 2#endif
When expression 1 is true, segment 1 is compiled, and when it is not, segment 2 is compiled. Conditional compilation can make the target program smaller and run time shorter. Precompilation increases the number of solutions to problems or algorithms and helps us choose the right solution. In addition, there is layout control: #progma, which is an important aspect of our application preprocessing. The main function is to provide the compiler with unconventional control flow information
.net also has its conditional compilation, but also the following processing format:
# # if# elif else # endif;

It's just that we seldom use it in our normal projects. For his specific blog to learn, please see:
  (link: #)

(3) function and class structure declaration, etc.

As for the declaration of functions and class structures, I really don't need to go into too much detail here. You understand it as an interface declaration and implement it in the source file.

Rival files say so much, but also do.net comparison. Let's sum it up in one sentence. Net actually has header files, but they are scattered in separate files, whereas C++ brings them together.

Related articles: