Solved

Program structure

Posted on 2006-06-24
4
218 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
[X]
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
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

Free Tool: Site Down Detector

Helpful to verify reports of your own downtime, or to double check a downed website you are trying to access.

One of a set of tools we are providing to everyone as a way of saying thank you for being a part of the community.

Question has a verified solution.

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

What is C++ STL?: STL stands for Standard Template Library and is a part of standard C++ libraries. It contains many useful data structures (containers) and algorithms, which can spare you a lot of the time. Today we will look at the STL Vector. …
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 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.

732 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