Solved

Program structure

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

How your wiki can always stay up-to-date

Quip doubles as a “living” wiki and a project management tool that evolves with your organization. As you finish projects in Quip, the work remains, easily accessible to all team members, new and old.
- Increase transparency
- Onboard new hires faster
- Access from mobile/offline

Join & Write a Comment

Article by: SunnyDark
This article's goal is to present you with an easy to use XML wrapper for C++ and also present some interesting techniques that you might use with MS C++. The reason I built this class is to ease the pain of using XML files with C++, since there is…
  Included as part of the C++ Standard Template Library (STL) is a collection of generic containers. Each of these containers serves a different purpose and has different pros and cons. It is often difficult to decide which container to use and …
The viewer will learn how to use the return statement in functions in C++. The video will also teach the user how to pass data to a function and have the function return data back for further processing.
The viewer will learn how to clear a vector as well as how to detect empty vectors in C++.

759 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

15 Experts available now in Live!

Get 1:1 Help Now