Solved

Program structure

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

Are your AD admin tools letting you down?

Managing Active Directory can get complicated.  Often, the native tools for managing AD are just not up to the task.  The largest Active Directory installations in the world have relied on one tool to manage their day-to-day administration tasks: Hyena. Start your trial today.

Question has a verified solution.

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

Suggested Solutions

Title # Comments Views Activity
why "." vs "->" 23 120
Excel/Word Add-in in what language? 5 118
FMX enumerated colours 2 100
convert char array to number in c 5 85
This article shows you how to optimize memory allocations in C++ using placement new. Applicable especially to usecases dealing with creation of large number of objects. A brief on problem: Lets take example problem for simplicity: - I have a G…
Go is an acronym of golang, is a programming language developed Google in 2007. Go is a new language that is mostly in the C family, with significant input from Pascal/Modula/Oberon family. Hence Go arisen as low-level language with fast compilation…
The goal of the video will be to teach the user the difference and consequence of passing data by value vs passing data by reference in C++. An example of passing data by value as well as an example of passing data by reference will be be given. Bot…
The viewer will learn how to user default arguments when defining functions. This method of defining functions will be contrasted with the non-default-argument of defining functions.

772 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