1

Looking around at different code bases I see a variety of styles:

  1. Class "interfaces" defined in header file and the actual impl in a cpp file. In this approach the headers look well defined and easy to read but the cpp files look confusing as it's just a list of methods.

  2. The second approach i see is just to put everything in a single class cpp file. These class files contain the definition and actual method impls in the body of the class definition. This approach looks better to me (more like Java and c#).

Which style should I be using?

Mat
  • 202,337
  • 40
  • 393
  • 406
CodingHero
  • 2,865
  • 6
  • 29
  • 42

7 Answers7

8

For all but the simplest programs, style #2 is simply impossible. If you #include a .cpp file with function definitions from multiple other .cpp files, the definitions get added to multiple object files (.o / .obj) and the linker will complain about clashing symbols.

Use style #1 and learn to live with the confusion.

Thomas
  • 174,939
  • 50
  • 355
  • 478
4

The former - interfaces in header files and class bodies in implementation files. You'll find this causes you fewer problems when working on large systems.

In C++ why have header files and cpp files?

Community
  • 1
  • 1
Trevor Tippins
  • 2,827
  • 14
  • 10
0

Header (HPP):
The header includes the declarations of your code, particularly function declarations. Technically speaking classes are defined in header-files, but again, the member functions are just declared.

Code in other files will include just this header and retain all necessary information from there.

Implementation (CPP):
The implementation includes the definition of functions, member-functions and variables.

Rationale:

  • Header-files gives a developer (a external user of your code) a plain overview and just offers the external available code (i.e. easy to read, only the information necessary for users).
  • Header-files allow the compiler to check the implementation for correctness
  • Header-files allow the compiler to check external code for correctness
  • Header-files allow for seperate-compilation. You need to keep in mind. that in former times, computers doesn't have enough resources to keep everything in main-memory during a compilation process. Header files are small, while implementation files are big.

Use #style 1, even for simple programs. So you can learn easily to work with. That maybe look outated today, especially in background of modern Multi-Pass-Compilers. But seperate header-files are even today beneficial. Rumours about the next C++-Standard appeared, as far as I know something like symbol export ( Java or C#) will be possible. But don't nail me down on this!

Notes:
- member-functions which are defined inside a class are by default inline, normally you don't want this
- use always defined guards

Peter
  • 2,240
  • 3
  • 23
  • 37
0

If you are developing large project, you'll find the first approach helps you a lot. The second approach may help you in small project. As your project becomes larger, management of complexity is a big issue of software development, and the first approach turns out to be a better choice.

K.C.
  • 2,084
  • 2
  • 25
  • 38
lilde90
  • 1
  • 1
0

What I do is:

This way I get:

  • fast compilation (just needs to recompile the .cpp file, unless I change the class interface)
  • few issues with circular declarations
  • acceptably low tedious mindless manual work :-)
Hugh Perkins
  • 7,975
  • 7
  • 63
  • 71
0

Since you tagged with c++, go for first style. I don't find it confusing, for a Java programmer, it may seem different, but in C++, you are always going to use this approach.

In fact in my favorite IDE (MSVS), I open header file, and cpp file side by side. Makes looking up prototypes, and class declaration easy.

And when you have a dozen classes; a dozen .h files, and another dozen .cpp file, will make your work simpler. Because, when you want just to see, what a class does, you just open relevant .h file, and take a look at class members, and maybe short comments. You don't need to wade through several lines deep code.

Conclusion : The style options you gave, are option only for a small code, typically single file, with very few methods etc. Otherwise, it is not even a option. (@Thomas has given the reason why #2 is not even a option)

Vinayak Garg
  • 6,518
  • 10
  • 53
  • 80
0

C++ doesn't use "interfaces" they use classes - base/derived classes. I use one file to define class/and its implementation methods if the project is small and separate files if the project is large. In java, I pack them up into one package then import it once in need.

user1058272
  • 113
  • 2
  • 9