The relationship between C + + CPP and HPP

Source: Internet
Author: User

 < Span style= "font-size:14px" > First, we can put everything in a. cpp file, which the compiler compiles into. obj, the compilation unit. one. cpp corresponds to one. obj and then links all. obj (through a program called a linker) to form an. exe, which is a program. if one. CPP uses another. CPP-defined function, just write its function declaration in this. cpp. The   the linker to link all obj, but what if it happens to have the same function or external variable? C + + can be qualified by a keyword called a link property, a function that is common to the entire program, or only used in a compilation unit obj, which is extern (external link) and static (internal link). Let's talk. H. Not really. h, the program works well, but when you find an externally linked function or external variable, you need a lot of sub-declarations, because whenever you use a unit of that function, you have to write a statement in that. cpp, if you want to modify it will be very troublesome!!! .h is born to solve this problem, it contains these common things, and then all the. cpp that needs to use this function, only need to be included in the # # include it can be modified later, Just modify a copy of the content. #include并不是什么申请指令, just copy the contents of the specified file in the intact.

Not very strictly speaking, the *.h file does the declaration of the class, including the definition of the class member and the declaration of the function, while the *.cpp file makes the concrete implementation of the class member function (definition). A *.h file and a *.cpp file are generally paired. In the first line of the *.cpp file is also commonly include "*.h" file, in fact, it is equivalent to the *.h file in the copy to the beginning of the *.cpp file. So, all you write in the *.cpp file is actually the same.

Since you can write CPP directly, why do you write hpp? In addition to programming specifications, it also involves an important property of the class, which is encapsulation. For example, now our company cooperates with another software company, so it is necessary to provide each other with some software information (such as some classes, what it is to do), but at the same time to provide this information and we do not like to let the other side know our specific implementation of these classes, after all, these are our company's algorithm core and effort AH. So this time you can put the interface of the class (What this class is to do) in the *.h file, and the implementation of the specific class is placed in the *.cpp file. At this time we just give the other company *.h file. This provides both the necessary information and the protection of our core code.

1. The most superficial mechanism is that the header file is the interface of the program (which is the Code interface), providing programmers with a series of declarations such as classes, templates, and functions, so that the programmer knows how to invoke the "things" inside.

2. From the point of view of the dynamic link library, the header file provides an interface that allows the programmer to find out how to load a function inside a dynamic library when it is necessary to load a library function (which is simply a simple example).

3. From the extension of the software: the header file as the interface, and then to define its implementation, so long as the interface is not changed (the header file is unchanged), you can only modify the implementation of the file, without having to modify other implementation code. For example, you have a sort () function for sorting, and in a large program you later find that the sort () has a better algorithm, So you just have to modify the implementation of the function (the code of the sort () function that modifies the. cpp file), and the other places where this function is used can be completely unchanged, which is the first benefit of the segmentation technique

4. From a compilation point of view:

All source files are compiled separately by the compiler, in the process of compiling, the header file is embedded in the implementation file together as a compilation unit is compiled (implementation of the file Filename.cpp # # "Filename.h" this line is ReplaceAll of the contents in the Filenam e.h (it actually removes the preprocessing instructions, which is the most essential function of preprocessing).

To give a simple example, you define the sort () function, which is declared in the Test.h header file, defined in the Test.cpp, at this time include "Test.h" in the Test.cpp, and define the sort () function.

You need to write the preprocessing code inside the header file

All contents of the header file must be included in the

#ifndef {Filename}
#define {Filename}

{Content of head file} Your code is written here

#endif

This ensures that when the header file is referenced by multiple other file references (include), the internal data is not defined more than once, causing an error.

It's the first two lines in the BLOB.HPP to see.

Last line:

What should the header file (. h) and source file (. cpp) in C + + write?

Header file (. h):

Write the declaration of a class (including the declaration of members and methods within a class), function prototypes, # define constants, etc., but generally do not write specific implementations

source file (. cpp):

The source file mainly writes the specific code that implements those functions that have been declared in the header file. It should be noted that the beginning must include the implementation of the header file, as well as the header file to be used. So when you need to use the class in your own header file, you just need to include it in.

Here's a simple example to describe, let's ask for a circle area.

The 1th step is to establish an empty project (for example in the VS2003 environment).

The 2nd step, create a new header file named Circle.h in the folder of the header file, its contents are as follows:

#ifndef Circle_h
#define Circle_h

Class Circle
{
Private
Double r;//radius
Public
Circle ();//constructor
Circle (double R);//constructor
Double area ();
};

#endif

Note The precompiled statement at the beginning of the end. In the header file, the specific implementation of the function is not written.

The 3rd step, to give a specific implementation of the Circle class, therefore, in the source folder to create a new Circle.cpp file, its contents are as follows:

#include "Circle.h"

Circle::circle ()
{
this->r=5.0;
}

Circle::circle (double R)
{
this->r=r;
}

Double Circle:: Area ()
{
return 3.14*r*r;
}

Note that the Circle.h is included at the beginning, in fact, as long as the files used in this CPP file are included! The name of this file is not necessarily called Circle.cpp, but it is highly recommended that the CPP file corresponds to the header file.

Finally, we build a main.cpp to test the Circle class we wrote, which reads:

#include <iostream>
#include "Circle.h"
using namespace Std;

int main ()
{
Circle C (3);
cout<< "Area=" <<c.area () <<endl;
return 1;
}

Notice that there is a "Circle.h" declaration at the beginning, proving that we have used the Circle class we just wrote.

At this point, the structure of our project is:

Run it and the output is:

Notice the difference between the declaration and the definition: the most essential difference is that the definition can occur only once, and the declaration can occur more than once. The declaration does not allocate space, and the definition is to allocate space.

http://blog.csdn.net/praker/article/details/38231757

Https://www.cnblogs.com/fenghuan/p/4794514.html

The relationship between C + + CPP and HPP

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.