I think TheIdeasMan's first post is the most important takeaway.
Asking
questions that are as focused as possible will get you
answers that are as focused as possible. If you ask too many questions in one post, it's hard for people to answer them without going into tangents (and for the questioner themselves to not go into tangents). Try to make your questions concise and without typos.
Here's my try, which I make no claim will be better than others:
What are header files generally used for? Shared code? What usually goes inside of the header files? I read function prototypes and classes, but im assuming anything o want to easily share among source files. |
Yes, generally you'd expect a header to be #included in more than one place. I think a better word than "shared" code would be
"reused" code. Functions and classes are really all you need to focus on at this point (and the occasional
enums and global constants).
The purpose is code organization. You could have everything in one huge file, but this is hard to scroll through and really just overwhelming. When your codebase grows, you'll have entire sections of the codebase that are dedicated to, say, the user interface, and another section dedicated to some SQL database, and another section dedicated to maybe networking. And each "section" of code may span multiple files. Having all of these different areas of the code all in one file would be very hard to reason about and organize.
i noticed that i need to include everything thats in the main source file, such as all the #includes etc because i was getting crazy errors |
This applies to many other areas of programming: Start small. Don't try to refactor all your code at once to use multiple files. Focus on just one class or function at the start. Start with a small program just to test out the idea of working with multiple files, following tutorials.
If you start out with a small program and experiment with it, others will be able to help you much easier when you have trouble.
Whats better practice [than global variables]? |
Pass variables by arguments. Encapsulate groups of variables that belong together in structs/classes, and then pass the single struct/class as an argument. Slowly build up from there.
Shouod i place them in a header file or a separate source file? |
The important thing is separating them out into different files. Whether you choose to have them all be inline in the header vs. separated into separate .cpp files is less important. There will be times when you'd be forced to separate out things into implementation files, so it's good to be familiar with it (such as two-way referencing, e.g. a Person needs to know what a Dog is, and a Dog needs to know what a Person is).