Purpose of .h files and .cpp files

Hello Everyone, I’m learning more than I ever thought possible! I have a questions/clarification. What is the true purpose of the header files and .cpp files. Here is what I think I understand.

.h files are a place in your code to ?declare? all your methods.

.cpp files are where all those methods are written.

I don’t know if I’m on the right track. If so why wouldn’t you just declare and write your methods in the .cpp file? Does it have to do with compile time? Or is it just standard practice to keep things tidy?

I’ve read that each section of a program might have different .h files for different sections, such as a .h file for AI in a game, or an .h file for player functions. Is this accurate? If I’m completely wrong please try your best to explain it to me as basic as you can.

Thanks everyone,

  • William

I’m fairly new to C++ so this might be off base, but most languages do this, C++ just does it a specific way because of history.

A lot of it deals with how the compiler works I am guessing. Header files are similar to interfaces it seems, if you know what interfaces are from higher level languages like Java. Simply put, they tell the compiler what it can and cannot do without knowing precisely how everything works, and thus doesn’t need to keep everything loaded in memory. This should also, in theory, allow the compiler to compile mostly baaed on those header files. As long as class/function signatures (function names, arguments, return types, variables, etc) don’t change, there is no need to re-build that section of code since the actual implementation is looked up as needed.

There are also some other benefits this provides, as it promotes you to think about what is implementation (what something does) and what is declaration (what something is capable of receiving/giving, what is available, etc). It helps with checking for logical errors and that kind of thing.

To give a little example, if you have a function called DoubleMe like in the course videos, and it takes an int and returns an int, you can change how you double something all you want. Since the declaration hasn’t changed, the compiler doesn’t need to care. The moment you change that function to DoubleIt, or make it return or take in something else, then it will need re-built since the rules have changed.

Again, I am pretty new to C++ and a lot of this is based off assumptions and what I know about programming, so feel free to correct any misunderstandings I have.

It has nothing to do with how the compiler works. Everything could be in one big fat file, all in one line, and the compiler wouldn’t care.

Headers and Code files are for the human mind.

Purpose; headers have a two-fold purpose:

  1. A single place to define something, like a class contract, constants, aliases, enums, etc. You want to ever define something - anything - just once.
  2. A way to re-use individual pieces of your code in other areas. You might want to use an error enum in another part of your application without the definition of a class that might be irrelevant in the other code. So keep everything separate.
1 Like

Are you sure about this? Most compile-time performance arguments revolve around header management that I’ve seen.

Few points here:

  1. Header files are “interfaces”. There is no need to know the actual implementation to use some function\method.
  2. In order to avoid unnecessary re-compilation when the implementation has been changed.

Details can be found here:

p.s. Diferentiating .h and .cpp files isn’t the finite way of organizing your code. There are pretty cool techniques to go even further, but those are used mostly in large projects where compilation might took hours (search for “PIMPL” conception).

Best regards.

There is actually another reason it works the way it does.

You see, the header files allow you to use other pieces of code (like libraries etc) without actually needing to compile it…

Libraries can be compiled to a shared object file (.so files in linux, .dll files on windows).
Then, you can just include the .h files and not worry about having to compile the shared object files every single time. This saves HUGE amounts of time for large projects.

In fact - even UE4 works this way. When you download UE4 through the launcher, you download a precompiled version of the engine, with the .h files that you need thrown in. This means you can code your c++ code against the UE4 engine without recompiling the UE4 engine every single time.

If you download the engine source from github and compile it yourself, you’ll know exactly why this is a good thing. On my PC it takes over 6 hours to compile the complete engine! Can you imagine having to recompile it every time you change something? That would make game development entirely impossible! But, because we can just include the .h files we don’t have to wait 6 hours every time we hit compile! It just uses the pre-compiled shared object / dll files and it only takes a couple seconds :slight_smile:

Privacy & Terms