# How many c++ classes per file

## Recommended Posts

Hey All, I was wondering what the standard convention was for using classes in c++. In Java and C# you typically put one class per file. You can create your final executable from these seperate files. Is this the same for c++. Is the <include> statement similar to the using keyword in c#? I was just thinking of making the switch to c++ and was curious Thanks T

##### Share on other sites
You typically declare only one class per .h file (and of course, you have one cpp file to define it).

Of course, when you have a bunch of really small classes that are closely related, you can do yourself a favor and put them in a single file.

Regards,

##### Share on other sites
I believe this is a matter of taste really. Personally, I tend to write classes in seperate files. Small structs, enums and constants that might be specific to a class I usually write together with the class.

I have no idea if this is the best way of doing it, but i've had no problems so far anyway.

##### Share on other sites
DarkZoulz, you live in Bålsta?

Anyway on topic, it depends like it has been said. I usually only have one "main" class per file, but when this class has some small utility classes/structs to support it I often put them into the same header (and .cpp) files.

##### Share on other sites
In Java you *can* put multiple classes per file, but only one can be public. Normally you *don't* do this because there's usually little reason to have non-public classes (except perhaps inner classes). But it really does come in handy sometimes.

Good C++ practice is probably analogous; normally you have one class per file, but 'helper' stuff can get bundled together. For example, if you have a really simple-interfaced (pretend that's a word for a moment please) class, you might put the base class and all derived versions in one file; maybe the derivations only override one virtual function, and the base implements some kind of factory such that outside users never need to know about the inheritance scheme. You would then want only the base class (and factory) to be 'public' in the Java sense; to get that effect in C++, you would simply only mention those items in the header (that might possibly require some forward declarations within the implementation file).

##### Share on other sites
Quote:
 Original post by Unwise owlDarkZoulz, you live in Bålsta?

I certainly do. =)

##### Share on other sites
Hey All,

Thanks for the info

T

##### Share on other sites
One more question. If I write a class in one .h file that inherits functions from a base class in another .h file, do I then need to use the <include "filename"> keyword where the base class is located or will the compiler know where to look?

T

##### Share on other sites
Quote:
 Original post by Long Dog StudiosOne more question. If I write a class in one .h file that inherits functions from a base class in another .h file, do I then need to use the keyword where the base class is located or will the compiler know where to look?T

You will need to include the header containing the base class. =)

##### Share on other sites
For me it really depends on the situation. As a general rule of thumb I try to have one class per .h/.cpp file. However, as people have mentioned before, small utility classes that have a strong relationship to the main class in the file I might put in the same file. Another case where I put several classes together was when I was defining exceptions for my game engine. I had all the child classes in the same .h/.cpp file because there were very few differences between them and it made it handy to include one file and get all the exceptions.

##### Share on other sites
What is the advantage of making a .cpp file to implement the .h if you are not including it inside the .h, but adding it to the project? I feel it is way better to do as I mentioned, since you can never have the implementation file without the declaration file.

// file.h#ifndef FILE_H#define FILE_Hclass SomeClass{    // ...};#include "file.impl"#endif

// file.implSomeClass::SomeClass(){    // ...}// ...

That's how I do.

##### Share on other sites
If you put a .cpp file instead of a .impl file like you do, the compiler recognizes it as a compilation unit. Files ending in .cpp are compiled, otherwise the compiler might ignore the code in your .impl file.

The only time I could think of where you would make a .impl file is if you're trying to separate the implementations of templated functions; most compilers can't handle having them in a separate compilation unit.

##### Share on other sites
Kalazart's example should result in a link time error if it's included by more than one file. If SomeClass was a template, then that would be an okay way to do things.

## Create an account

Register a new account

• ### Forum Statistics

• Total Topics
628293
• Total Posts
2981870

• 11
• 10
• 10
• 11
• 17