2

After tutoring a few students at a local college, I was asked "when is best to use a header file in C++ compared to a regular .cpp file?". I kind of struggled with the answer and was seeing if there is a more definite answer of what is best under which scenarios.

Is a header file best used when using code that can be used in multiple projects? I know this is a stupid example but if you made custom math functions that could be used over and over in other projects also; that would be best placed in a header file correct?

Martin Liversage
  • 104,481
  • 22
  • 209
  • 256
Falcon165o
  • 2,875
  • 1
  • 20
  • 31
  • 1
    Have a look at [this](http://stackoverflow.com/questions/1945846/c-what-should-go-into-an-h-file) and [this](http://stackoverflow.com/questions/333889/in-c-why-have-header-files-and-cpp-files) and [this](http://stackoverflow.com/questions/1167875/header-per-source-file) and... well, you get the point. – Bart Feb 27 '12 at 15:40
  • and this:http://stackoverflow.com/questions/280033/c-header-files-code-separation – Naveen Feb 27 '12 at 15:47

5 Answers5

10

Anything that needs to be accessed from more than one .cpp file should get declared in a header file, and the header file should be #included into each relevant .cpp file.

This can, for example, include declaration of global variables, global functions, classes etc.

NPE
  • 486,780
  • 108
  • 951
  • 1,012
1

headers files enable a better reuse of developed code (i.e. functions or classes). As a matter of fact is a standard practice to always define a .h and cpp file for each class you define. The answer to your second question is yes, if you want to reuse custom functions along your projects you may define its signature in a .h file and its actual implementation in a .cpp file or in an already compiled dll/lib

Andrea Sindico
  • 7,358
  • 6
  • 47
  • 84
1

If a function, a class or a macro or something else is needed to be reusable (it might be accessed from numbers of different files), then a good approach is to put it in a header file and let other files to include that file.

ahmet
  • 646
  • 6
  • 14
0

The header defines the interface; the source file provides the implementation. Except for trivially simple test programs, you generally start with the header—and only write the implementation once the header is done. In practice, you may come back and modify the header; e.g. to add private members to a class. But the goal is to correctly define what you need to implement before trying to implement it, and that's the role of the header.

Another frequent policy is to put the low level documentation in the header as well. In such cases, when writing the header, you'll start by writing a description of what the class will do—its role and its responsibilities, in particular—before writing any actual C++ code.

James Kanze
  • 150,581
  • 18
  • 184
  • 329
0

In C++ header files are used for declaration, where as in most cases source files are used for defining.

Where you need to use a class in more than one file you need to provide the class declaration to each one, in that case it is best to use header files otherwise if you use source files, you will end up with multiple definitions and on top of that the size of the executable will increase.

In general it is good practice to use header files for every class, or sometimes a group of related classes.

ahj
  • 745
  • 1
  • 6
  • 13