Hello,

As a beginner, I am finding it difficult to understand the function of header files, why they are used, and what you should include in them. I don't really know the difference between a .h file and a .cpp file.

Suppose I have a class MyClass, with a member variable, int MyVariable, and a member function MyFunction. I could write this in a .h file:

Code:
class MyClass
{
int MyVariable;
void MyFunction()
{
// Do something
}
};
And if I #include this .h file at the beginning of my source code, then I can use this class with no problems.

However, I have noticed that many projects will only put the decleration in the .h file, and the definition will appear in a .cpp file. What is the point in this? As far as I'm concerned, it just makes everything more dispersed and harder to follow.

Other times, the definition is included in the .h file, as above, and this seems like the sensible way of going about it.

Perhaps it is something to do with how the compiler interprets .h and .cpp files, but I don't really know what the difference is between them.

Please help