In C++ (using Unreal Engine of course) what goes into a header file vs a cpp file? What is the difference between the two

Hi everybody,

I am trying to create a sort of guard-like AI character for a school assignment. I did it once using blueprints, but unfortuantely we are not allowed to use blueprints, only C++. In blueprints I used a function called “OnSeePawn” which I can see is used in C++ but I don’t know which file to use it in. I have never seen a .h and .cpp before. The C++ I used was just all one file. You would declare all your variables and stuff then make classes, functions, or whatever. To make a long question short, what is a .cpp and .h file. I know .h is a header file but what exactly is that? What do you use in a header that you don’t use in a cpp?

That is the documentation that I am trying to use if you want to base your answer off these functions and stuff.

There are tons of answers on the web already, this is not different in UE from “normal c++” so
I’m sending you a few links with more in depth info.

https://www.youtube.com/watch?v=RMnpwICsgYo

https://www.quora.com/What-is-the-difference-between-a-cpp-file-and-a-h-file

c++ - What is the difference between a .cpp file and a .h file? - Stack Overflow

In header files you declare classes and methods, which you implement in a .cpp file. You can include header files and should never include a .cpp file. Modifying a single header file means everything will have to be recompiled. Modifying a single .cpp file does not. In a header you can (often) forward declare classes you are going to use without actually using "include …h’ this improves compile time. In the .cpp file where you actually implement things you should then include the headers of the forward declared classes.

2 Likes

In general in C/C++, your declarations of things are in header files, and the definitions of things are in the code files.

2 Likes