How Many Classes Per File?

Started by
6 comments, last by hapaboy55 20 years, 1 month ago
Hi all, I come from a Java background where each class is usually contained in its own file. I''m wondering, when it comes to C++, do people usually seperate each class into its own header and cpp file, or are multiple classes contained within the same header and cpp file? I was taking a look at some of the built in Visual Studio libaries and they seem to do the later. Do most C++ programmers follow this style as well?
Advertisement
I prefer one class per .cpp/.hpp but situations vary sometimes. (Dependant classes often get shoved in the parent class''s file.-- I don''t like saving one source file to find it depends on another file )
I use one class per .h. Then I define everything from that class in a .cpp. I like to do this because it keeps your code very organized. You can use more than one class in a file with C++, but it gets very bulky when you have a program which uses 15 classes. I suggest that you code the way you code in java. It will keep your code neat and working.


Favorite Quotes:Gandalf: You shall not pass!|Smeagol: We don''t need you!|Sloth: Hey you guys!|
Favorite Quotes:Gandalf: You cannot pass!|Smeagol: We don't need you!|Sloth: Hey you guys!|
Ein cpp, ein header, ein führer!
--AnkhSVN - A Visual Studio .NET Addin for the Subversion version control system.[Project site] [IRC channel] [Blog]
One cpp + h per class seems the most logical and organized way. Also, in some cases you''d prefear to define 2 or more in the same file, if those classes are very related to each other.

But I think it''s more of a personal choice, it will work the same way whatever you use one or ten files.
-----DevZing Blog (in Spanish)
I''m not too organized, but usually I use one class per file, but with any structs it uses in the same file. For instance, my renderer class also has geometry, material, staticbuffer, and dynamicbuffer structures defined in the same file, but they have no methods.

~CGameProgrammer( );

Screenshots of your games or desktop captures -- Post screenshots of your projects. There''s already 134 screenshot posts.
~CGameProgrammer( );Developer Image Exchange -- New Features: Upload screenshots of your games (size is unlimited) and upload the game itself (up to 10MB). Free. No registration needed.
I''ve had extremely complex code with up to 10 classes in a file. Especially in my GUI code. You know how many files it would take to contain all of those control types?

Also, sometimes things get extra detailed, like having character classes, character animation classes, and character mode classes. Unless the code grew too fast, I would usually put all of them into a single source/header combo.

I think it''s fine to group related objects. But it''s difficult to find certain objects if the code becomes large. It''s also fine to put every object into it''s own files. Buy then it''s difficult to find objects when you have too many of them
Generally, I keep it to one class per file, but if two classes are very closely related, I will put them in the same file.

Closely related classes are classes that will only be used by a parent class, and that have relitivly few functions of their own. For example, if you have a class called Object3D that stores vertices and faces in a vertex Class and a face class, you might want to put the vertex Class and face Class in the same file.

This topic is closed to new replies.

Advertisement