Solved

Program structure

Posted on 2006-06-24
4
214 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

Is Your Active Directory as Secure as You Think?

More than 75% of all records are compromised because of the loss or theft of a privileged credential. Experts have been exploring Active Directory infrastructure to identify key threats and establish best practices for keeping data safe. Attend this month’s webinar to learn more.

Question has a verified solution.

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

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…
C++ Properties One feature missing from standard C++ that you will find in many other Object Oriented Programming languages is something called a Property (http://www.experts-exchange.com/Programming/Languages/CPP/A_3912-Object-Properties-in-C.ht…
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.

895 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

Need Help in Real-Time?

Connect with top rated Experts

17 Experts available now in Live!

Get 1:1 Help Now