You are importing text. What the compiler does is simplified the same as you copying and pasting the text that is inside the include into the text that is in the main font.
This is why the position in which it is included is important. Almost always when you do not do it at first you are in trouble.
It's importing all your content. Normally this content is just declarations of data structures (classes, structures, enumerations), constants, function / method header, macros and eventually some code when you want a function to be imported directly into the source instead of being called ( source inline ). The most common is that it does not have code. Usually it is used to declare the data structures but not to define the algorithms.
Ideally, do not abuse includes within includes . This is not always possible.
In this example the compiler will probably mount something like this to compile:
/*
todo conteúdo enorme do arquivo string.h aqui
*/
using namespace std;
string a() {
return "PTSO";
}
/*
todo conteúdo enorme do arquivo string.h aqui repetido já que ele já está lá em cima
*/
using namespace std;
int main() {
return 0;
}
There is a technique for avoiding the repeated inclusion of include files by accident. Which does not seem to be the case. Well, it's because you did not know you were wrong. But it is a case that you did deliberately. The crash occurs when it becomes complex and you can no longer control "eye" an include is already there in the code or not.
I will consider that this is just an example, because it makes no sense to use #include "Header.h"
in the main code. It is including something that is not necessary.
Anyway, it would be normal to set the function a()
within .cpp
itself.
Whenever you can avoid using include , avoid. Of course in a real application this is almost impossible. Creating an include is required when you are going to use the algorithms defined in a .cpp
in another source file. When you are sure to only use it inside yourself, you do not need to create a .hpp
.
A better example
auxiliary.cpp
#include <string>
std::string a() {
return "PTSO";
}
auxiliary.hpp
#include <string>
std::string a();
main.cpp
#include <iostream>
#include "auxiliar.hpp"
int main() {
cout << a();
return 0;
}
Note that in C ++ it is common to use .hpp
, although a lot of programmer does not even try to reuse in C. What would you do with both extensions as well. I'd rather use .hpp
to make it clear that code was written in C ++.