[Home]
[Edit this page]
[Recent Changes]
[Special Pages]
[Help]
gwbasic » compile » CPU cycles » BeginnersGuideToPHP » AtariATW » CppTemplate » ComputerScienceFAQ » Brainfuck » MSDOS-Basics » iTV » CppHeaderFile
Header files often consist of functions or class ?declarations and or definitons. Often they have the .h or .hpp extension. Header files from the STL must be called without this .h extension.
Sometimes a header (.h) file also has an implementation (.cpp) file with the same name. When this is the case, the header file contains declarations and the implementation file the definitions. A header file and its implementation file are sometimes called a Unit.
Note that the third #include first checks if the header file is in the same folder as the program. If it is not present, it will check the standard header folder for it.
Always use #include guards [1].
[Edit this page] [Page history] [What links here] [Discuss this topic] [Printer Friendly]
gwbasic » compile » CPU cycles » BeginnersGuideToPHP » AtariATW » CppTemplate » ComputerScienceFAQ » Brainfuck » MSDOS-Basics » iTV » CppHeaderFile
(C++) Header file
A file called in your program, using the #include statement.Header files often consist of functions or class ?declarations and or definitons. Often they have the .h or .hpp extension. Header files from the STL must be called without this .h extension.
Sometimes a header (.h) file also has an implementation (.cpp) file with the same name. When this is the case, the header file contains declarations and the implementation file the definitions. A header file and its implementation file are sometimes called a Unit.
- include <iostream> //STL header
- include <myOwnCoolHeaderFile.h> //Header in standard folder
- include "myOwnCoolHeaderFile.h" //Local header
Note that the third #include first checks if the header file is in the same folder as the program. If it is not present, it will check the standard header folder for it.
Always use #include guards [1].
Use of header and implementation files
There are three ways to put your code in header (.h) and implementation (.cpp) files:- 1) Use only .h files
- 2) Add .cpp files to project
- 3) #include all .cpp files from the file that contains main()
1) Use only .h files
This option is used by many libraries (most notably the STL and most parts of Boost) put all definitions in the .h files. This has the advantage that they are easy to call: you only need to #include the necessary .h files.//Example.h
- ifndef __EXAMPLE_H
- define __EXAMPLE_H
struct Example { void sayHello() const { std::cout << "Hello" << std::endl; //Definition in .h file } };
- include <iostream>
- endif
//Main.cppint main() { Example myExample; myExample.sayHello(); }
- include "Example.h"
2) Add .cpp files to project
This option is used in larger non-library code. The header files contain the declarations, the implementation files the definitions. The .cpp files must be 'Added to Project'. How this is done is dependent on the programming environment. The advantage is that compiling is quickest.//Example.hstruct Example { void sayHello() const; };
- ifndef __EXAMPLE_H
- define __EXAMPLE_H
- endif
//Example.cppvoid Example::sayHello() const { std::cout << "Hello" << std::endl; }
- include "Example.h"
- include <iostream>
//Main.cppint main() { Example myExample; myExample.sayHello(); }
- include "Example.h"
3) #include all .cpp files from the file that contains main()
This option can be used in example source code. It enables newbies to compile-and-run.//Example.hstruct Example { void sayHello() const; };
- ifndef __EXAMPLE_H
- define __EXAMPLE_H
- endif
//Example.cppvoid Example::sayHello() const { std::cout << "Hello" << std::endl; }
- include "Example.h"
- include <iostream>
//Main.cpp
#include "Example.cpp" //Calling .cpp file
int main()
{
Example myExample;
myExample.sayHello();
}
C++ Library headers
International C++ Standard, table 11- algorithm
- bitset
- complex
- deque
- exception
- fstream
- functional
- iomanip
- ios
- iosfwd
- iostream
- istream
- iterator
- limits
- list
- locale
- map
- memory
- new
- numeric
- ostream
- queue
- set
- sstream
- stack
- stdexcept
- streambuf
- string
- strstream
- typeinfo
- utility
- valarray
- vector
C++ Headers for C library facilities
International C++ Standard, table 12- cassert
- cctype
- cerrno
- cfloat
- ciso646
- climits
- clocale
- cmath
- csetjmp
- csignal
- cstdarg
- cstddef
- cstdio
- cstdlib
- cstring
- ctime
- cwchar
- cwctype
Incomplete list of non-standard header files
'Header' links
Reference
- 1) Herb Sutter and Andrei Alexandrescu. C++ coding standards: 101 rules, guidelines, and best practices. ISBN: 0-32-111358-6. Chapter 24: 'Always write internal #include guards. Never write external #include guards'.
[Edit this page] [Page history] [What links here] [Discuss this topic] [Printer Friendly]
