• 14
• 12
• 9
• 10
• 13

# When to use .h vs .cpp?

This topic is 3829 days old which is more than the 365 day threshold we allow for new replies. Please post a new topic.

## Recommended Posts

I have read a lot on C++ lately and as much as I am trying to learn, this tends to be one thing that I cannot find an answer for. What do you put into a header file vs what do you put in a source file? Im aware that header files are called in at the top of other files, but is there a certain type of data or information you put into one as opposed to the other? The only information I have been able to figure out is that for every source file there is a header file? This leads me to believe that maybe the constants, variables, etc. are all in the header file?

##### Share on other sites
A source file doesn't have to correspond to one or more header files. A header file is usually the interface for a compiliation unit (.cpp file). That's usually the class definition and function prototypes. The .cpp file would then provide the implementation for all the functions and class(es) in the header.

##### Share on other sites
Quote:
 Original post by CrimsonGTWhat do you put into a header file vs what do you put in a source file?

You place declarations in the header file and definitions in the source file. The entire reason header files exist is to allow you share declarations between translation units.

Quote:
 The only information I have been able to figure out is that for every source file there is a header file?

No.

Organizing C and C++ Code Files - required reading.

##### Share on other sites
You put in a header file only what you have to. It should be things like class definitions that need to be shared between cpp files, or anything else that need to be shared between cpp files.
Otherwise, put stuff into the cpp file where you can.

It's not unusual to have many more header files than cpp files either, but you don't tend to have a cpp file without a corresponding header file. Somehow its contents have to be used elsewhere in the program, which you expose through the header.

##### Share on other sites
Quote:
 Original post by iMalcYou put in a header file only what you have to. It should be things like class definitions...

Declarations. If you put definitions in headers and include them in multiple source files, you'll get redefinition linker errors.

##### Share on other sites
Class definitions can go into header files. The class definition is the part that looks like:
class MyClass {  public:    MyClass();    // other stuff};

##### Share on other sites
I consider that the declaration. If you have:
class MyClass{public:    MyClass();    int MyFunction() const;private:    int member;};

You can't build with the above, if an instance of MyClass is created and used. That's symptomatic of a declaration, in my opinion. You need definitions for the constructor and MyFunction.

Plus, as I stated earlier, if you have a declaration in a header and you include said header in two source files, you'll have redefinition errors when you link. To me that's the key (and that's the origin of the term redefinition error, anyway).

##### Share on other sites
Unfortunately, the C++ standard disagrees with you. See chapter 9 paragraph 2.

##### Share on other sites
I agree with Oluseyi although sometimes it is required to put definitions into headerfiles, like when using template functions/template classes or inlines.
To get rid of the redefinition error just use
#pragma once with Visual C++ or put the declarations inside a #ifndef-block like in

#ifndef SOME_DEFINE
#define SOME_DEFINE

// declarations

#endif

See http://en.wikipedia.org/wiki/Include_guard for an explanation

##### Share on other sites
There's a really good thread on this subject here. It should answer just about all your questions and refers to some other references on this website.

As to whether .h contains declarations or definitions or complete class and method definitions, it depends upon context. For example, with templates it is generally a good idea to include complete definitions of classes, methods and functions. For other (non-template) classes or functions, the class definition should be included in the header, and class declarations can sometimes be used where pimpl-idioms (or composition by pointer) are used. Functions are declared, and global variables are extern'ed. This is all explained in the referenced thread. Function definitions and class method definitions are placed in the .cpp files. To understand this, it is important to understand what happens during compilation and linking. You must be careful not to define methods and functions more than once.

As you can see, it is important that you get a clear understanding of what represents a definition and what represents a declaration, and what extern means, in the context of namespaces, classes, functions and variables.

As for me, I use .h files to collect like groups of 'operating headers' together, normally by namespace. I call 'operating headers' as those headers that contain definitions of classes and templates and declarations of functions. These are placed in .hpp files. And then the compilation units (source code files) are provided in .cpp files.

--random