Archived

This topic is now archived and is closed to further replies.

How Many Classes Per File?

This topic is 5043 days old which is more than the 365 day threshold we allow for new replies. Please post a new topic.

If you intended to correct an error in the post then please contact us.

Recommended Posts

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?

Share this post


Link to post
Share on other sites
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 )

Share this post


Link to post
Share on other sites
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!|

Share this post


Link to post
Share on other sites
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.

Share this post


Link to post
Share on other sites
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.

Share this post


Link to post
Share on other sites
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

Share this post


Link to post
Share on other sites
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.

Share this post


Link to post
Share on other sites