Hi Everyone,

I have been trying to teach myself how to code for more than a year now, having started with VBA and then switching to c++. Recently I have been forced to confront how horrific my coding practices are, and have been doing pennance by trying to modify 1500 lines of spaghetti code for a different, but related project. My questions is not about any specific code, but rather seeking guidance on how a project is best organized.

I understand that class declarations go into header files and contain function prototypes, but where should I flesh out the function? Previously I had just thrown everything into "main.cpp" and just stacked modules as high as I had to. What belongs in "main.cpp"? What should be thrown into other ".cpp" or ".h" files? Should the function "main" be a minimal as possible? When is it a good idea to link other ".cpp" files to main?

Apologies for the generality of the question, but the book I bought doesn't seem to discuss this level of organization, and the web forums also seem relatively silent on the subject.

Thanks in advance,

Baldur

Usually, people just put the main() function in main.cpp (and sometimes a function or two that will only ever be used by main()), and leave other functions in other files.

The place where you actually "flesh out" the function, as you put it, is called the "implementation" of the function (at least where I come from). Implementations of functions other than main() belong in other .cpp files. How you organize those .cpp files depends on how you're writing your program, really.

When you're doing object oriented programing, it's common to see a class get it's own header and it's own matching .cpp file. For example, if I have a class called Ball, I will put the declaration of the class in "Ball.h", and the implementations of its member functions in "Ball.cpp".

Generally this is how it goes, even outside of object oriented programming. If you have a bunch of functions that deal with crunching numbers for geometry, perhaps you should throw those into a "geometry_math.cpp" and "geometry_math.h".

Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.