Hello! I want to try to make a Snake Game. And I want this game to have menu which contains "Play", "Options", "High Scores" and "Exit".
When "Play" is chosen the game will start. When the "Options" is chosen there will be another menu and the user will need to select the game difficulty. When "High Scores" is chosen the player will see his previous scores - They will be read from a file.
My question is: What structure should I use. I want the "Options"'s functions to be in different file and the "High Scores"'s function to be in different file too. I can make a class with all these functions and just define them in different header and .cpp files.
I can also just make them different functions but it's not recommended to declare global variables in header files.
What do you think? Should I make one class and just define it's method's in different files?
Greetings!
Why would you want to split the class source code across several files. It's normal practice to have a separate file for the class header and another file for the source code for that class. Try to avoid #includes in the header file and keep those to the source files.
> Try to avoid #includes in the header file and keep those to the source files.
It would be better if you could simply use your header.
Forcing the sources to include other headers prior to yours is error prone and subject to change.
> Try to avoid #includes in the header file and keep those to the source files.
It would be better if you could simply use your header.
Forcing the sources to include other headers prior to yours is error prone and subject to change.
The header should include only those other header files which are necessary for the contents of that header file to compile.
It should not include a load of other header files just because it's convenient to have them all in one place. That leads to unnecessary coupling, and increased compilation times.
Note that you can reduce the number of #include statements in a header file, by using forward declaration in cases where your header file doesn't need a complete definition of a type. That can cut down on your header file coupling significantly.
So, in my case it is better to have everything declared in one header and defined in one .cpp file? :)
There's nothing wrong with breaking down your code into smaller sub-units. In fact, generally it's a good idea, assuming you do it sensibly.
I wouldn't, however, recommend taking the code that defines a single class and spreading it over several source code files. That way leads to confusion.
Instead, if your class is so big that it's helpful to split it up, then it's worth splitting it up into several smaller classes. Each class should still have a single .h and a single .cpp.