im curious if there is any type of standard....or if anyone could help explain what all should be placed in .H files? (prototypes/etc) -thx

anything except executable code/functions. There is no standard that says what you can and can not put in heder files -- its just common sense.

The .H (header) file is more of a code organisation means to make your code readable and easily maintained. Logically, declarations and definitions are placed in the header file to allow an easy glance over the codes. All implementation codes shall go to the CPP file (with same corresponding name)

It's a matter of style, and of cause you should code with style :)

In an object oriented environment you want to encapsulate the logic in a class and only expose to the outside world, what they really need to know to use the class, no more. So in my view :

Use as few includes of other .h files as possible in a header file. This will decrease be number of header files you need to distribute if you want to share your classes with others, or share between projects.

Second don't do any coding in the header file. If you want to in-line some code, do it in the implementation file with the in-line directive.

Many includes can be avoided by using forward declarations and encapsulating of data in data structures.

Example.

// Some class .h
class A {
public:
A();
~A();
};

// Some other class .h
#include a.h
class B {
public:
B();
~B();
private:
A a;
};

In this example you will need access to both a.h and b.h to use the B class elsewhere. A better solution for B would be :

// Some other class .h
class A; // Forward declaration, does not require include of a.h
class B {
public:
B();
~B();
private:
A *a; // prt can be declared (not initialized) if A is forward declared.
};

//in the .cpp
#include a.h // hidden to the surrounding world
#include b.h
B::B() {
a = new A();
}

Btw you cannot seperate the implementation and the decleration if you are working with a template class.

This is an accepted practice...Don't use any form of

using namespace_name;


in a header file.

>>Second don't do any coding in the header file. If you want to in-line some code, do it in the implementation file with the in-line directive.

Here I disagree -- again matter of style, but I find it a lot easier to just put inline code in the class where its declared

class Hello
{
   void SayHello() { cout << "Hello\n"; }
};

If you let Microsoft VC++ 2010 compiler generate CLR/C++ project the IDE will put a lot of inline code inside the header files it generates.

So far, I think all the above answers are very good and have all the essential ideas. One additional guideline I can think of is about preserving interfaces (but probably doesn't concern you too much at this point, it's more for bigger projects). There are programming techniques such as some forms of Cheshire cats (i.e. the PImpl idiom) and binary compatible interfaces that have more strict requirements for what can go in the header files (or at least the published headers). Basically, the more code / complications you put in your header file, the more likely it is that you might have to fix a problem with it later, and then you "break" the interface, often requiring or at least triggering global recompilations (which are extremely annoying in big projects!).

One caveat:
From Tellalca:>>Btw you cannot seperate the implementation and the decleration if you are working with a template class.
I have to disagree with that. First off, yes, you can separate implementation from declaration of class or function templates (made possible by either explicit instantiations or including .cpp files). Second, it is a good idea to do so for all the same reasons that apply to "plain" code, however, people typically accept more code in a header file for a class template if it is simple enough. In other words:
In plain code, only the very trivial functions can (and probably should) be implemented in the header file, while the rest is in the cpp.
In templated code, only if the function or class template is "too complicated" should it be implemented in a .cpp file (by "too complicated" I mean that the implementations are too lengthy and break readability). If all functions are fairly small (like 5 to 10 lines at max), then it is probably easier to put it all in the header file.
There are, of course, exceptions to those rules for special circumstances (implementing a PImpled class template for example).

Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.