c++ A header/cpp for each class or not?

Started by
7 comments, last by Qw3r7yU10p! 19 years, 7 months ago
Hi with classes in c++, should i have a header/cpp file for each class or should i put things like eg: Render, Font, Model classes inside the Render.h or a seperate Font.h,.cppetc What do you thinks best?
Advertisement
I would say Render, Model...
The second option. It is what I do.

EDIT: All rendering related classes in render.h/cpp.
All ____ (fill in the blank) in ____.h/cpp
Group tightly related classes together, generally each should have it's own .cpp and typically it's own .hpp with maybe a common interface header.
- The trade-off between price and quality does not exist in Japan. Rather, the idea that high quality brings on cost reduction is widely accepted.-- Tajima & Matsubara
also how about the winmain, do you put that in a main.cpp or something
WinMain can be defined in the same module (.cpp file) as your main class. You can have a seperate main.cpp or entry.cpp, too but from my experience WinMain is very short and needs the defintion of your main class anyway. So I don't see any advantage of having it in a seperate file.

[edit]Typos - I'd better get some sleep [wink][/edit]

Pat.
what I tend to do is put all the class headers in one header (ClassDef.h) and then put all the function definitions in seperate files (aka, font.cpp, render.cpp, coolgfx.cpp), that way I can easily open one file, and look up the functions for the classes, without searching for the right file.
"Not all who wander are lost."- J.R.R. Tolkien
Quote:Original post by Virus2566
what I tend to do is put all the class headers in one header (ClassDef.h) and then put all the function definitions in seperate files (aka, font.cpp, render.cpp, coolgfx.cpp), that way I can easily open one file, and look up the functions for the classes, without searching for the right file.


But if you change anything in the header you have to recompile every cpp file
I prefer the .cpp/.h combination, however, I don't bother with a header file for main.cpp, and if a class is very small, or a pure virtual class I don't make a .cpp for it.
This is mainly to keep compile time down to an absolute minimum, but in the end it boils down to personal preference, there is no 'Right way' to store c++ code.

Tristam MacDonald. Ex-BigTech Software Engineer. Future farmer. [https://trist.am]

Quote:Original post by swiftcoder
in the end it boils down to personal preference, there is no 'Right way' to store c++ code.


Personal preference isn't the bottom line when you're working with a team or coming to an existing project.

Industry practice is to seperate into .cpp and .h for each class. That would be the most common approach.

This topic is closed to new replies.

Advertisement