How Many Classes Per File?
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?
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 shall not pass!|Smeagol: We don''t need you!|Sloth: Hey you guys!|
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.
But I think it''s more of a personal choice, it will work the same way whatever you use one or ten files.
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( );
-- Post screenshots of your projects. There''s already 134 screenshot posts.
~CGameProgrammer( );
-- Post screenshots of your projects. There''s already 134 screenshot posts.
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
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.
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
Popular Topics
Advertisement