2

my first project in c++ (I use g++ on centos)

from Python and c#, I use to split my program into few files, each class in different file.
The most common reason i find, why use .h file, is because you can change the header code in the .cpp file, without affecting other people that uses that specific header. this feature is meaningless when using version control, where no one see the changes until you submit your code to the main project library. (#ifndef can still be used to prevent multiple main() and such errors) Besides that, I cant find reason why I should use .h files to write classes in different files.
I can just use

#include "class1.cpp" 
#include "class2.cpp"

I will get the same result as using .h file, without the need to maintain classes in two different files (.h + .cpp) (and without the complexity when using default values) As far as I understand, in both cases, the compiler will include the source code, as if it is one big source file. So.. why should I use .h (and .cpp) files and not just split my program into .cpp file instead ?

orenk
  • 109
  • 1
  • 2
  • 12
  • 3
    You should never include `.cpp` file. – drescherjm Feb 17 '20 at 13:55
  • 1
    What happens when you have dependencies between classes? What happens to your build times when you have 1000 source files all included in a single file and you make changes? What about the readers of your code? – drescherjm Feb 17 '20 at 13:57
  • 1
    A lot of things don't seem to make sense in C++ if you're only working with "toy" projects (a handful of classes). Also the C heritage is quite heavy. – Mat Feb 17 '20 at 13:59
  • @drescherjm one might include a .cpp to instantiate templated classes in a shared library for example – hetepeperfan Feb 17 '20 at 13:59
  • In that case I would not name the file that implements a template a `.cpp` file – drescherjm Feb 17 '20 at 14:00
  • @drescherjm I wouldn't mention the readers of the code as an argument. Plenty of languages can live well without enforced interface- (aka header-)files. – AlexGeorg Feb 17 '20 at 14:01
  • 2
    "...is because you can change the header code in the .cpp file, without affecting other people that uses that specific header. this feature is meaningless when using version control" - It is not like you are saying. The advantage is that nobody needs to see the raw implementation of functions in the class, so the changes or details of implementation are invisible to the header user, so he cand and is forced to work only with the knowledge available on .h file. – Felipe Matos Mendes Feb 17 '20 at 14:04
  • @FelipeMatosMendes This is not true. They can see the implementation by simply opening the .cpp file. How does the use of a header file prevent them from seeing the implementation? – user253751 Feb 17 '20 at 14:32
  • @Mat The linked duplicate basically addresses: why you need header files when you are compiling cpp files separately. It does not address: why cpp files should be compiled separately. – user253751 Feb 17 '20 at 15:05
  • 1
    @user253751: OP is already talking about splitting his cpp files. But more generally: look at the source code for some non-trivial C++ programs (Qt, KDE, or Chrome for example). Having a single cpp file weighing in at several hundred megabytes is simply unmanageable. How you compile (one at a time, in batches, in parallel, distributed, whatever) is an engineering choice you make (with the help of your build system). – Mat Feb 17 '20 at 15:11
  • 1
    @Mat So then the answer is "because it would compile too slowly otherwise" – user253751 Feb 17 '20 at 15:14
  • @user253751 If the .cpp file is provided you can see it. If you have only .o file you do not know exactly how it was implemented. – Felipe Matos Mendes Feb 17 '20 at 15:22
  • @FelipeMatosMendes So, one reason to use separate compilation units is "so I can hide my code from other people." This does not apply to your own project. Why should orenk use separate compilation units on his/her own project? – user253751 Feb 17 '20 at 15:26
  • 1
    @user253751 It is good practice. It is not mandatory. If you separate the code in the two files you separate declaration and implementation. Is a way to say "to use this classe I need to know just what is defined in .h file" – Felipe Matos Mendes Feb 17 '20 at 20:02
  • Thank you all. from all your answers, my conclusion is that its not mandatory to use reader files. For big projects and for code hiding, it is better to use .h files, but for small projects, its less important. a good practice is to use header file. – orenk Feb 18 '20 at 09:14

0 Answers0