[Home]  [Edit this page]  [Recent Changes]  [Special Pages]  [Help
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.

  1. include <iostream> //STL header
  2. include <myOwnCoolHeaderFile.h> //Header in standard folder
  3. 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
  1. ifndef __EXAMPLE_H
  2. define __EXAMPLE_H
  1. include <iostream>
struct Example { void sayHello() const { std::cout << "Hello" << std::endl; //Definition in .h file } };
  1. endif


//Main.cpp
  1. include "Example.h"
int main() { Example myExample; myExample.sayHello(); }


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.h
  1. ifndef __EXAMPLE_H
  2. define __EXAMPLE_H
struct Example { void sayHello() const; };
  1. endif


//Example.cpp
  1. include "Example.h"
  2. include <iostream>
void Example::sayHello() const { std::cout << "Hello" << std::endl; }


//Main.cpp
  1. include "Example.h"
int main() { Example myExample; myExample.sayHello(); }


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.h
  1. ifndef __EXAMPLE_H
  2. define __EXAMPLE_H
struct Example { void sayHello() const; };
  1. endif


//Example.cpp
  1. include "Example.h"
  2. include <iostream>
void Example::sayHello() const { std::cout << "Hello" << std::endl; }


//Main.cpp
#include "Example.cpp" //Calling .cpp file
int main()
{
  Example myExample;
  myExample.sayHello();
}


C++ Library headers

International C++ Standard, table 11

  1. algorithm
  2. bitset
  3. complex
  4. deque
  5. exception
  6. fstream
  7. functional
  8. iomanip
  9. ios
  10. iosfwd
  11. iostream
  12. istream
  13. iterator
  14. limits
  15. list
  16. locale
  17. map
  18. memory
  19. new
  20. numeric
  21. ostream
  22. queue
  23. set
  24. sstream
  25. stack
  26. stdexcept
  27. streambuf
  28. string
  29. strstream
  30. typeinfo
  31. utility
  32. valarray
  33. vector

C++ Headers for C library facilities

International C++ Standard, table 12
  1. cassert
  2. cctype
  3. cerrno
  4. cfloat
  5. ciso646
  6. climits
  7. clocale
  8. cmath
  9. csetjmp
  10. csignal
  11. cstdarg
  12. cstddef
  13. cstdio
  14. cstdlib
  15. cstring
  16. ctime
  17. cwchar
  18. cwctype

Incomplete list of non-standard header files

'Header' links

Reference



last edited (November 27, 2006) by bilderbikkel, Number of views: 12310, Current Rev: 30 (Diff)

[Edit this page]  [Page history]  [What links here]  [Discuss this topic]  [Printer Friendly]  

Members

Username:

Password:


Register
Forgot Password?




Programmers Heaven - for .NET, Java, C/C++ and WEB Developers!
© 1996-2008 Community Networks Ltd. All rights reserved. Reproduction in whole or in part, in any form or medium without express written permission is prohibited. Violators of this policy may be subject to legal action. Please read Terms Of Use and Privacy Statement for more information. Development by Tore Nestenius at .NET Consultant - Synchron Data.