-1

I have a project that contains several classes. I added to this project several functions that are used by one of the classes, let named MainClass. I put all these functions in a file stuff.h. To use these functions I include the stuff.h file at the beginning of the implementation of the class MainClass. I am wondering if this is a good style of programming or if it would be better to create a new class and add my functions to this class. Then I instantiate this class in the MainClass.

Charlie
  • 11,380
  • 19
  • 83
  • 138

2 Answers2

0

Not so good style. Starting from the fact headers are not actually intended to have real code but declarations of things defined somewhere else.

If your 'external' functions are used only by MainClass why not to do them class methods? Even maybe private so they are only visible inside class? Keep things as encapsulated as you can. If you're trying to follow C++, try not to use 'plain C functions'. It's different language. If you absolutely need plain routines, use namespaces instead. But please try to keep your code inside modules (.cpp), not in headers.

Regarding other classes. It depends if you know why you need other classes. If you don't know why, you don't need them. BTW OOP is not always 'best' approach, especially in things like balance between inheritance and composition. You should understand what you really want to achieve to select proper technique.

Indeed you need good C++ book. Chapters about project composition, compilation process, translation units so you will understand logics behind this. This Q&A site cannot explain you everything. Just give some points.

Community
  • 1
  • 1
Roman Nikitchenko
  • 12,800
  • 7
  • 74
  • 110
  • So I just put declarations in stuff.h and put the implementation in a stuff.cpp file. Is putting these functions in a separate class a bad idea? Thanks. – user3026374 Dec 15 '13 at 01:45
  • If only MainClass needs something, why it cannot be part of MainClass? OK, if you see component logics, start new class module, let say UtilityClass.cpp, implement code inside class or namespace and add UtilityClass.h through inclusion of which MainClass can see its interface. It's about general design of C++ projects. Read books, you cannot avoid it :-). – Roman Nikitchenko Dec 15 '13 at 01:54
  • It looks from your answer that it would be better to add these functions to the MainClass. After reflection it is not a such bad idea. Is there a restriction on the number of functions that a class should contain? – user3026374 Dec 15 '13 at 01:58
  • Technically you should not meet any limits here but from other side you need to keep things in order. So from other side you should keep balance here. If there is too much functions inside MainClass, think about grouping them and forming other interfaces (classes or namespaces). Usually it's recommended not to have modules larger than 2000 lines but mostly its very subjective. – Roman Nikitchenko Dec 15 '13 at 13:58
  • You can see this question is on hold. Not because it's too bad but because its provocative. – Roman Nikitchenko Dec 15 '13 at 14:00
0

I think it would be better to create a new class, or several new classes that are ordered by the different ways the functions will be used. That's more in line with classic OO.

anyaelise
  • 92
  • 1
  • 7