How to create an aquivo. h?

Asked

Viewed 1,560 times

11

What good is a arquivo.h and what improvement it brings to the program in C++?

  • 5

    It would be better to clarify what you want to know because it is the same way as any other file.

2 answers

16


The files with extension .h or .hpp As some like to differentiate from C header files, they are usually used to place code needed to compile other parts of an application.

When we create an application, or even a library, some information is needed to compile. Some examples are classes, structures, static constants and the declaration of functions (so-called prototypes). In the case of classes and structures we are talking about several elements, including signature of methods.

Basically what is not necessary for the compilation are the algorithms, ie the code within the functions or methods. These can be compiled for binary code and are no longer required for the build process. At least in most cases.

There are still cases where you want to do the inline of the code or it is gabaritado (template).

This file is a way to organize the code and facilitate the inclusion of this code in other files.

Contrary to what some may think, this file has normal code that might well be in a file .cpp. Of course, if all these quoted statements are in the file .cpp flexibility is lost. It becomes complicated to use your content elsewhere.

A file that includes a .hpp after pre-processed have the contents of the .hpp as always written on it. That is, the file is as if copied to the file that will be compiled.

So in the file .cpp you have the definition algorithms and the .hpp you have the statement. Note that this is a universally adopted convention but nothing prevents you from doing it in a different way. There’s just no reason to think of any other way. With it you can have gains in compile time in addition to organizing better what data structure and what is algorithm.

If you have a file .cpp thus:

void teste();

void main() {
    teste();
}

void teste() {
    printf("teste");

You can only use the function teste() in this file or else need to include the code together, which may complicate by existing the main(), besides spending time compiling something that should already be compiled. Of course it is possible to save (#ifdef) the snippet not to include what it does not need, but it may end up complicated the normal compilation of the file and puts thing in the file that are not necessary for it.

Break up like this in the teste.hpp:

void teste();

And the main.cpp being like this:

void main() {
    teste();
}

void teste() {
    printf("teste");

You can use the function teste() anywhere else. Let’s imagine that you create a new file .cpp thus:

#include "teste.hpp"
void funcao() {
    teste();
}

This works because the function declaration will be included in the file, even if the actual code of the function is not. After pre-processing this code will be:

void teste();
void funcao() {
    teste();
}

I put in the Github for future reference.

4

My friend, the file. h is a header, a configuration file, there you can set constants, include libraries, things that will be used in your main (main program). You create a . h and include in your main program.

Separating such features, you have greater maintainability in your source code, for example if you want to use another library just insert it in the . h and automatically your program will use it, and you can also use the same . h in other programs.

I hope I’ve helped.

Browser other questions tagged

You are not signed in. Login or sign up in order to post.