If you have two CPP files one called Main.cpp and one called Second.cpp, in Second.cpp you need to include <iostream> again that you already have included in Main.cpp if you use std::cout for example.
My question is when the compiler compiles everything it goes from up to down from the beginning in Main.cpp to Second.cpp wouldn't the compiler understand that <iostream> is already included? Or is it the an IDE error?
This part of the file says: if the variable IOSTREAM_H is not defined, then define it, and do the stuff in the //file contents. This is analysed at compile-time, NOT at run-time.
With this header in each header file, you won't have the problem of including stuff more than once. Actually if you deliberately include/define stuff more than once without such cautions, the compiler will definitely complain about it.
I'm aware of that, I'm just curious about why it needs to be included two times? From what I understand is that the compiler just put all text together. Same as if you put an int in Main.cpp and tries to use it in Second.cpp. You'll still need to use a pointer or the extern command to use the int in Second.cpp.
My question is when the compiler compiles everything it goes from up to down from the beginning in Main.cpp to Second.cpp wouldn't the compiler understand that <iostream> is already included?
This is wrong. It does not go from the beginning in Main.cpp to Second.cpp . It goes from the start of Main.cpp to the end of Main.cpp ,and then stops. Then, it goes from the start of Second.cpp to the end of Second.cpp. They are compiled completely independently. Anything you put in Main.cpp has no bearing whatsoever on the compilation of Second.cpp
They are compiled as separate files. In Main.cpp, when the compiler gets to the function call second(), it does NOT go to a different file to compile that. It happily notes that there is a function call there, and carries on with the file it is doing.