Header files contain the declarations of functions and classes.
Header files commonly have the .h and .hpp filename extensions.
To use a header file, it must be #included in the source code.
#include <iostream>
#include "Widget.h"
int main()
{
Widget w;
std::cout << "Hello world\n";
}A header (.h) file can (and commonly does) start with an #include guard. The combination of a header (.h) file and an implementation (.cpp) file is called a unit.
This example is copied from [17].
//radio.h
#ifndef INCLUDED_RADIO
#define INCLUDED_RADIO
int z; // illegal: external data definition
extern int LENGTH = 10; // illegal: external data definition
const int WIDTH = 5; // avoid: constant data definition
static int y; // avoid: static data definition
static void func() {/*...*/} // avoid: static function definition
class Radio
{
static int s_count; // fine: static member declaration
static const double S_PI; // fine: static const member declaration
int d_size; // fine: member data definition
// ...
public:
int size() const; // fine: member function declaration
// ...
}; // fine: class definition
inline int Radio::size() const
{
return d_size;
} // fine: inline function definition
int Radio::s_count; // illegal: static member definition
double Radio::S_PI = 3.14159265358; // illegal: static const member definition
int Radio::size() const { /*...*/ } // illegal: member function definition
#endif // INCLUDED_RADIOStandard header files
The STL consists out of the following header files [3][4]:

algorithm
array
atomic
bitset
cassert
ccomplex
cctype
cerrno
cfenv
cfloat
chrono
cinttypes
ciso646
climits
clocale
cmath
codecvt
complex
condition_variable
csetjmp
csignal
cstdalign
cstdarg
cstdbool
cstddef
cstdint
cstdio
cstlib
cstring
ctime
complex
ctgmath
ctime
cuchar
cwchar
cwctype
deque
exception
forward_list
fstream
functional
future
initializer_list
iomanip
ios
iosfwd
iostream
istream
iterator
limits
list
locale
map
memory
mutex
new
numeric
ostream
queue
random
ratio
regex
set
sstream
stack
stdexcept
streambuf
string
strstream
system_error
thread
tuple
typeindex
typeinfo
type_traits
utility
valarray
vector
unordered_map
unordered_set
- Make header files self-sufficient [1,14]
- Don't put a using-directive in a header file [18,19]
- Avoid non-inline function definitions in header files [7,9,12]
- #include C header files in namespace to avoid global names [13]. For example, #include cmath instead of math.h
- include user-defined headers' names in quotes, include STL headers' names in angle brackets [15]
- Short functions should be inlined and defined in headers [16]
- Large functions should be declared in headers and defined in source files [16]
- Herb Sutter, Andrei Alexandrescu. C++ coding standards: 101 rules, guidelines, and best practices. 2005. ISBN: 0-32-111358-6. Item 23: 'Make header files self-sufficient'.
- Herb Sutter, Andrei Alexandrescu. C++ coding standards: 101 rules, guidelines, and best practices. 2005. ISBN: 0-32-111358-6. Item 24: 'Always write interal #include guards. Never write external #include guards'.
- International C++ Standard, table 11
- Draft of C++0x Standard, table 14 and 15, ISO/IEC JTC1 SC22 WG21 N 3290, Date: 2011-04-11, ISO/IEC FDIS 14882, ISO/IEC JTC1 SC22
- John Lakos. Large-Scale C++ Software Design. 1996. ISBN: 0-201-63362-0. Chapter 1.1.3, figure 1-3, page 28
- John Lakos. Large-Scale C++ Software Design. 1996. ISBN: 0-201-63362-0. Section D.2: Major Design Rules, Chapter 2, page 820: 'Avoid free functions (except operator functions) at file scope in .h files'
- Bjarne Stroustrup. The C++ Programming Language (3rd edition). 1997. ISBN: 0-201-88954-4. Section 9.5, item 4: 'Avoid non-inline function definitions in headers'
- John Lakos. Large-Scale C++ Software Design. 1996. ISBN: 0-201-63362-0. Section 2.5, page 85: 'Place a redundant (external) include guard around each preprocessor include directive in every header file'
- Joint Strike Fighter Air Vehicle C++ Coding Standards for the System Development and Demonstration Program. Document Number 2RDU00001 Rev C. December 2005. AV Rule 39: 'Header files (*.h) will not contain non-const variable definitions or function definitions.'
- Bjarne Stroustrup. The C++ Programming Language (4th edition). 2013. ISBN: 978-0-321-56384-2. Chapter 15.5. Advice. page 444: '[1] Use header files to represent interfaces and to emphasize logical structure'
- Bjarne Stroustrup. The C++ Programming Language (4th edition). 2013. ISBN: 978-0-321-56384-2. Chapter 15.5. Advice. page 444: '[2] #include a header in the source file that implements its functions'
- Bjarne Stroustrup. The C++ Programming Language (4th edition). 2013. ISBN: 978-0-321-56384-2. Chapter 15.5. Advice. page 444: '[4] Avoid non-inline function definitions in headers'
- Bjarne Stroustrup. The C++ Programming Language (4th edition). 2013. ISBN: 978-0-321-56384-2. Chapter 15.5. Advice. page 444: '[8] #include C headers in namespace to avoid global names'
- Bjarne Stroustrup. The C++ Programming Language (4th edition). 2013. ISBN: 978-0-321-56384-2. Chapter 15.5. Advice. page 444: '[9] Make headers self-contained'
- Paul Deitel, Harvey Deitel. C++11 for programmers (2nd edition). 2014. ISBN: 978-0-13-343985-4. Chapter 3.6, Error-Prevention Tip 3.3. page 57: 'To ensure that the preprocessor can locate headers correctly, #include preprocessing directives should place user-defined headers names in quotes [...] and place C++ Standard Library headers names in angle brackets [...]'
- Gottschling, Peter. Discovering Modern C++: An Intensive Course for Scientists, Engineers, and Programmers. Addison-Wesley Professional, 2015. Chapter 7.2.3.2: 'Short functions should be inline and defined in headers. Large functions should be declared in headers and defined in source files'
- John Lakos. Large-Scale C++ Software Design. 1996. ISBN: 0-201-63362-0. Chapter 1.1.3, Figure 1-3, page 28
- Bjarne Stroustrup. The C++ Programming Language (4th edition). 2013. ISBN: 978-0-321-56384-2. Chapter 14.5. Advice. page 417: '[11] Don't put a using-directive in a header file'
- Herb Sutter, Andrei Alexandrescu. C++ coding standards: 101 rules, guidelines, and best practices. 2005. ISBN: 0-32-111358-6. Item 59: 'Don't write namespace usings in a header file or before an #include'