Solved

Program structure

Posted on 2006-06-24
4
217 Views
Last Modified: 2010-04-01
I'd be interested to hear how you/[your development team] typically structure your software...

- Do you define all constants and prototypes in Header files?
- Do you declare/define namespaces in headers also? Or entirely in source files?
- Do you try and give each class it's own file? Or do you prefer to group classes within a single source file?
- etc

Is there a conventional way of structuring your programs, which all [good] programmers comply with?

Thanks
0
Comment
Question by:InteractiveMind
4 Comments
 
LVL 86

Accepted Solution

by:
jkr earned 80 total points
ID: 16975901
>>- Do you define all constants and prototypes in Header files?

If applicable - yes (exception: Functions that are only used at file scope and not in any other module)

>>- Do you declare/define namespaces in headers also? Or entirely in source files?

Since namespaces are bound to the declarations, you even have to. But, refrain from intruducing a

using namespace xyz;

in header file, since that will affect all source files that include it. Naming ambiguities are looming.

>>- Do you try and give each class it's own file? Or do you prefer to group classes within a single
>>source file?

That's nice, but IMO not necessary. Probably depends on one's personal style, I prefer "functional grouping".
0
 
LVL 16

Assisted Solution

by:PaulCaswell
PaulCaswell earned 75 total points
ID: 16975976
Hi Rob,

>>- Do you define all constants and prototypes in Header files?
No. The header is the outward export of the module. You should only put stuff in your header that should/can be used by other modules. In the interest of reducing scope/visibility as much as possible keep everything that others dont NEED to know in the source module. I will often do a lot of work just to hide something.

Stuff like:

class aClass;

class MyClass {
private aClass * anObject;
...

This way I even hide all details of the inner object.

- Do you declare/define namespaces in headers also? Or entirely in source files?
I dont use C++ often enough to give a definitive suggestion here.

- Do you try and give each class it's own file? Or do you prefer to group classes within a single source file?
Usually, but not always. Helper classes are a good example.

- etc

Is there a conventional way of structuring your programs, which all [good] programmers comply with?
Not really. I usually try to put the stuff most people will want to see at the top, like constructors and primary functionality. Getters and setters at the end.

Paul
0
 
LVL 30

Assisted Solution

by:Axter
Axter earned 30 total points
ID: 16976960
>>- Do you try and give each class it's own file?

One of the few things I like about Java, is that each class has it's own file, and the file name is the class name.
I think this is a good idea, and it makes it easy to find your code, and easy to read the code.
0
 
LVL 48

Assisted Solution

by:AlexFM
AlexFM earned 65 total points
ID: 16977626
About namespaces: if you write some kind of class library, using namespaces in modern C++ is desired. Namespaces hierarchy is built completely in h-files, for example:

// Filter.h

namespace ImageProcessing
{
    namespace Filters
    {
        class CFilter
        {
            public:
                CFilter();
        };
    }
}

In your own implementation files, as mentioned by jkr, you can add "using" lines:

// Filter.cpp
#include "Filter.h"

using namespace ImageProcessing::Filters;

CFilter::CFilter()
{
}

Without "using" you should write:

ImageProcessing::Filters::CFilter::CFilter()
{
}

But "using" lines should never be used in header files.
0

Featured Post

Independent Software Vendors: We Want Your Opinion

We value your feedback.

Take our survey and automatically be enter to win anyone of the following:
Yeti Cooler, Amazon eGift Card, and Movie eGift Card!

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

Suggested Solutions

Unlike C#, C++ doesn't have native support for sealing classes (so they cannot be sub-classed). At the cost of a virtual base class pointer it is possible to implement a pseudo sealing mechanism The trick is to virtually inherit from a base class…
Often, when implementing a feature, you won't know how certain events should be handled at the point where they occur and you'd rather defer to the user of your function or class. For example, a XML parser will extract a tag from the source code, wh…
The goal of the video will be to teach the user the concept of local variables and scope. An example of a locally defined variable will be given as well as an explanation of what scope is in C++. The local variable and concept of scope will be relat…
The viewer will learn additional member functions of the vector class. Specifically, the capacity and swap member functions will be introduced.

685 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question