Go back to Richel Bilderbeek's homepage.
Go back to Richel Bilderbeek's C++ page.
#include is a preprocessor directive to add a header file (.h or .hpp) or other files to your program.
There are multiple forms of a header file #includes:
#include <iostream> // 1: STL |
The first, without the .h extension, means that this header file is from the Standard Template Library. The second #include means that the header is not from the STL but in the standard header file directory. The third #include means that the file is local, that is in the same directory as the program. If the local #include fails, the standard header file directory is checked for this header file.
Never #include unnecessary header files [1]. Prefer to #include <iosfwd> when a forward declaration of a stream will suffice [2]. Never #include a header when a forward declaration will suffice [3]. The implementation (.cpp) file of every component should #include its own header (.h) file as the first substantive line of code [7].
For backwards compatibility with C one can #include C++ header files with the .h extension. Do not do this: call the correct C++ header file. For a list of all C++ standard header files, go to the header file page.
#include <stdio.h> //C-stle #include, avoid to do this |
The header file 'stdio.h' is a wrapper: all it does is call the C++ header file cstdio and then adds a 'using namespace std', as C does not have namespaces.
This has the unfortunate side-effect that after calling such a header file all functions and classes in namespace std will be in the global namespace.
As it pollutes the global namespace, avoid using namespace std [4-5].
An example from [6]:
#include <vector> //Carefully avoids polluting the global namespace |
Conside not #including these [8], but add these to your project instead. For example, in C++ Builder, select 'Project | Add to Project'.
Forgetting to add an implementation file to you project results in a link error.
Go back to Richel Bilderbeek's C++ page.
Go back to Richel Bilderbeek's homepage.
This page has been created by the tool CodeToHtml