Architecture of a c++ program

Hello everyone.

I'm a fairly experinced java programmer, but to broaden my knowledge of programming in general i recently decided to learn c++ (or atleast to understand it).

Im doing alright on the semantic/syntatic part of it, but i have some trouble structuring my programs correctly - maybe because i think too much like a java programmer?

So, to be a little more specific i've been working on the logic for a http://en.wikipedia.org/wiki/Connect_Four game.

I've decided on the classes:
Game: to represent the board
Brick:
Player: To represent the player in turn
Position: A struct to encapsulate lookups in the board

Resulting in class diagram looking like this: http://i.imgur.com/hs6A1By.jpg
(The dependency arrows might have to be association arrows?)

Anyways, my question is now, how would i structure this in files? I've read that it's a good pratice to represent each class as a header file with declartions and a .cpp file with the full definition. Unless the class represents an interface/abstract class, then i only need to represent it with a header file?

That leaves me with the following list of files:

Headers:
Game.h
Game_class.h
Brick.h
Player.h
Player_class.h
Position.h

Cpp files:
Game_class.cpp
Brick.cpp
Player_class.cpp
Position.cpp

Now, for the sake of arguement, say that i decide to add an enumeration "Color", to distinguish the players and the bricks. This means that both Player and Brick have to implement the same header Color.h. And that i need a new file "just" to represent a simple enum? Is that really best pratice? I feel like a program made with that mindset could grow into the thousands of files fairly quickly?


Also i know that the interfaces are a little overkill in this example, but i need them to illustrate my point.




I would try to make it without classes first.
@fabtasticwill
What? Why would you say that?

@MTorp
Typically there will be a header that collects random program stuff like that. So that's fine, having a header that simply enumerates colors, like Colors.h or Globals.h.

C++ doesn't require you to sign everything in triplicate, but it does require you to dot the I's and cross the T's. It's structure isn't significantly different than in Java.

Hope this helps.
Last edited on
@Duoas
C++ doesn't require you to sign everything in triplicate, but it does require you to dot the I's and cross the T's. It's structure isn't significantly different than in Java.


I'm not entirely sure i know what you mean? Am i free to design the program as i'd like as long as it's semantically correct?
Basically, yes.

C and C++ require everything you use to be known beforehand. So if you want to use a function or a class or even a variable before it is defined (or if it has been defined elsewhere) then you must tell the compiler what it looks like. That is the purpose of header files.

You might want to read through the FAQ about file types and purposes and basic project organization here:
http://www.cplusplus.com/faq/compiling/files/

There's also a link to the bottom of that for an example multi-file project (short and silly, but with valuable commentary).

I think it will help you a lot, coming from Java.
You might want to read through the FAQ about file types and purposes and basic project organization here:
http://www.cplusplus.com/faq/compiling/files/


Thank you, that was very helpful.
Topic archived. No new replies allowed.