C ++ .cpp and distinguish the .hpp

Original Address: https://blog.csdn.net/qzx9059/article/details/89210571

c ++ cpp and the hpp
we can put everything in one .cpp file, the compiler will compile the .cpp to .obj, namely compilation unit. A program may be composed of a coding unit, may compile a plurality of constituent units. A .cpp corresponding to a .obj, then all the .obj linked (through a program called the linker) to form a .exe, namely program. If you use a .cpp .cpp defined function to another how to do, just write in the .cpp its function declaration. The linker will link up all the obj, but if you happen to have the same function or external variable is how to do? C ++ may be defined by keywords called link attribute is a function belongs to the common procedure, or only used in a compilation unit obj inside, these keywords is extern (external link), and static (internal links). Let's talk about .h. In fact, there is no .h, the program can also work well, but when you find a link to an external function or external variable declaration requires a lot of points, because as long as the unit to use this function, you must write a statement in the .cpp inside, if you want to modify a lot of trouble! ! ! .h is to solve this problem born, it contains these common things, then all need to use the function of .cpp, just use #include can be included into the future needs to be modified, just modify a content. What #include directive does not apply, but the contents of the specified file, intact copy come.
Not very strictly speaking, .h files do is kind of statements, including statements and function definitions of class members, and the specific implementation .cpp file to do the class member functions (definitions). A * .h files and * .cpp file is usually paired. In the first line of the * .cpp files are generally #include ".h" file, in fact, equivalent to copy .h file to the beginning of things * .cpp files. So, you write all * .cpp file is actually the same.
Since you can write directly to cpp, why write hpp? In addition to programming specification, a further important property related to the class, is the encapsulation. For example, now we have another company and software companies, so they are bound to each other to provide some information about the software (such as some class, which in the end is what to do), but at the same time to provide this information, we know that we are not like the other side specific implementation of these classes, after all, these are our company's core algorithms and effort ah. So this time we can put interface class (this class is what to do) on the * .h file, and the achievement of specific class in the .cpp file. This time we just give the other company .h file on the line. This will not only provide the necessary information, but also to protect our core code.
1. The outermost surface of the mechanism are: the header file is the interface program (the code interface) available to the programmer to declare a series of classes, templates, functions, etc., so that the programmer should know how to call inside the "stuff."
2. From the perspective of the dynamic link library to see: provides an interface header file, so that programmers need to load a library function of time (this is only cite a simple example) to view the header files to know how to load the function of the internal dynamic library.
3. From the expansion of the software is: as an interface header file, go to the definition of its implementation, so long as the same interface (header files intact), you can modify only the implementation file, without having to modify other implementation code. For example, you have a sort () function sorts, in a large program, you later find that the sort () has a better algorithm, so you just need to modify the function of the realization (sort modify .cpp file () function Code), other places can use this function completely unchanged, this is the first benefit segmentation techniques
4. Compiled from the perspective of:
all source files are compiled unit are respectively divided respectively compilation, during compilation, the header file is embedded in the implementation file which is compiled together as a compilation unit (in the implementation file filename.cpp #include "filename.h" is replaced with the line inside all to filename.h (preprocessing directives will actually removed, this is the most essential preconditioning)).
As a simple example, you define the sort () function, in test.h header file declarations defined in test.cpp years, this time #include "test.h" in test.cpp inside, and define the sort () function.
You need to write the code inside the pre-header file
header file of all the content, must be included in
#ifndef {Filename}
#define {Filename}
12
// File} {Content of your head to write the code here
#endif
so as to ensure when headers are multiple references to other files (include), internal data will not be defined more than once caused the error.
c ++ header files (.h) and source files (.cpp) should write about?
Header (.h):
declared write class (including declaration inside the class members and methods), the function prototype, # define constants, but generally does not write specific implementation.
Header files can also define: at compile time already know cosnt objects and inline a function of its value. Entity defined in the header file above, because they are needed to define the compiler to generate code.
Source file (.cpp):
Source file written primarily to achieve specific code for those functions in the header file has been declared. Note that, the beginning of what must #include the header file to achieve, and to use header files. So when you need to use to write the class header file, just #include came on the line.
Will hereinafter be described, let's find a circular area cite a simple example.
 Step 1, create an empty project (with an example in the VS2003 environment).
 Step 2, in the file header of the file folder named Circle.h create a header file, which reads as follows:
#ifndef CIRCLE_H
#define CIRCLE_H
class Circle
{
Private:
    Double R & lt; // radius
public:
    Circle (); // constructor
    Circle (double R); // constructor
    double area (); // function mensuration
};
#endif
12345678910111213141516
the beginning of the end of the notice prepared statement. In the header file, do not write the specific implementation of the function.
Step 3, to give a specific realization of the Circle class, and therefore, a new file in the source Circle.cpp folder, which reads as follows:
#include "Circle.h"
Circle::Circle()
{
    this->r=5.0;
}
Circle::Circle(double R)
{
    this->r=R;
}
Circle Area :: Double ()
{
    return 3.14 * r * r;
}
123456789101112131415161718
should be noted that: at the beginning contains Circle.h, in fact, as long as the cpp file used files, must include it! In fact, the name of the file does not have to be called Circle.cpp, but highly recommended cpp file and header files, respectively.
Finally, we build a main.cpp to test our written Circle class, which reads as follows:
#include <iostream>
#include "Circle.h"
a using namespace std;
main int ()
{
    Circle C (. 3);
    COUT << "Area =" << c.Area () << endl;
    return. 1;
}
have #include "Circle.h" declaration of the beginning of the note, we demonstrated we just write to the use of the Circle class.
1234567891011
At this point, the structure of our project are:
Note the difference between declarations and definitions here: they are the most essential difference is that the definition can only occur once, the statement may appear more than once. Declaration does not allocate space, and the definition is to be allocated space.
----------------
Disclaimer: This article is CSDN bloggers "clear away light" original article, follow the CC 4.0 BY-SA copyright agreement, reproduced, please attach the original source link and this statement.
Original link: https: //blog.csdn.net/qzx9059/article/details/89210571

Guess you like

Origin www.cnblogs.com/lzhu/p/11970591.html