home > resources > c++ coding standard > online version

Class Files

  • Each class definition should be in its own file where each file is named directly after the class's name.
  • Source files have the .cpp extension and header files have the .h extension.
  • In general each class should be implemented in one source file. A common exception to this rule are inner classes that provide class specific functionality such as thread execution. Another common exception are very closely related classes such as a collection class and its iterator.
  • If the source file gets too large or you want to avoid compiling templates all the time then add additional files, where the section is lower case and seperated of the classname through an underscore.

Justification

  • Using exactly the same name as the real class makes it easy to establish the relation.
  • Not implementing several classes in one source file makes it very easy to find a class implementation when looking for it.

Example 2-2. Class Files Example

ClassName.h
ClassName.cpp
ClassName_section1.cpp
ClassName_section2.cpp