Skip to content

Latest commit

 

History

History
274 lines (251 loc) · 13.3 KB

File metadata and controls

274 lines (251 loc) · 13.3 KB

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_RADIO

Standard header files

The STL consists out of the following header files [3][4]:

  1. C++98C++11 algorithm
  2.  C++11 array
  3.  C++11 atomic
  4. C++98C++11 bitset
  5.  C++11 cassert
  6.  C++11 ccomplex
  7.  C++11 cctype
  8.  C++11 cerrno
  9.  C++11 cfenv
  10.  C++11 cfloat
  11.  C++11 chrono
  12.  C++11 cinttypes
  13.  C++11 ciso646
  14.  C++11 climits
  15.  C++11 clocale
  16.  C++11 cmath
  17.  C++11 codecvt
  18.  C++11 complex
  19.  C++11 condition_variable
  20.  C++11 csetjmp
  21.  C++11 csignal
  22.  C++11 cstdalign
  23.  C++11 cstdarg
  24.  C++11 cstdbool
  25.  C++11 cstddef
  26.  C++11 cstdint
  27.  C++11 cstdio
  28.  C++11 cstlib
  29.  C++11 cstring
  30.  C++11 ctime
  31. C++98C++11 complex
  32.  C++11 ctgmath
  33.  C++11 ctime
  34.  C++11 cuchar
  35.  C++11 cwchar
  36.  C++11 cwctype
  37. C++98C++11 deque
  38. C++98C++11 exception
  39.  C++11 forward_list
  40. C++98C++11 fstream
  41. C++98C++11 functional
  42.  C++11 future
  43.  C++11 initializer_list
  44. C++98C++11 iomanip
  45. C++98C++11 ios
  46. C++98C++11 iosfwd
  47. C++98C++11 iostream
  48. C++98C++11 istream
  49. C++98C++11 iterator
  50. C++98C++11 limits
  51. C++98C++11 list
  52. C++98C++11 locale
  53. C++98C++11 map
  54. C++98C++11 memory
  55.  C++11 mutex
  56. C++98C++11 new
  57. C++98C++11 numeric
  58. C++98C++11 ostream
  59. C++98C++11 queue
  60.  C++11 random
  61.  C++11 ratio
  62.  C++11 regex
  63. C++98C++11 set
  64. C++98C++11 sstream
  65. C++98C++11 stack
  66. C++98C++11 stdexcept
  67. C++98C++11 streambuf
  68. C++98C++11 string
  69. C++98C++11 strstream
  70.  C++11 system_error
  71.  C++11 thread
  72.  C++11 tuple
  73.  C++11 typeindex
  74. C++98C++11 typeinfo
  75.  C++11 type_traits
  76. C++98C++11 utility
  77. C++98C++11 valarray
  78. C++98C++11 vector
  79.  C++11 unordered_map
  80.  C++11 unordered_set
  1. 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'.
  2. 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'.
  3. International C++ Standard, table 11
  4. 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
  5. John Lakos. Large-Scale C++ Software Design. 1996. ISBN: 0-201-63362-0. Chapter 1.1.3, figure 1-3, page 28
  6. 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'
  7. 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'
  8. 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'
  9. 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.'
  10. 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'
  11. 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'
  12. 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'
  13. 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'
  14. 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'
  15. 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 [...]'
  16. 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'
  17. John Lakos. Large-Scale C++ Software Design. 1996. ISBN: 0-201-63362-0. Chapter 1.1.3, Figure 1-3, page 28
  18. 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'
  19. 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'