0

I may be thinking/using header files the wrong way, but currently I'm using 1 .cpp file and the rest are all in header files. I have things like a screen which is an array of characters in a header file, I have the function to draw the screen to the terminal in a header file, I have all my global variables in a header file, but I have game.cpp where all those files will be used.

I am doing this wrong? I feel like at this point there's no reason to use .cpp files. Header files seem to be so much better because I avoid the issue of redeclaration.

Gavin C.
  • 105
  • 8
  • If you're fine with recompiling all your source code every time you need to compile, and you're also fine with serializing that compilation, sure, put everything in a single giant CPP file. But if you split source code into multiple CPP files, it means that a change in one doesn't require you to recompile another that doesn't depend on it, and if you have a multithreaded system it means you can compile multiple translation units in parallel. It also can improve readability if you have multiple files that are individually smaller and represent some coherent piece of the project. – Nathan Pierson Apr 17 '21 at 03:48
  • You won't notice much of a benefit until your projects start getting up into the tens and twenties of files. By the time you get into building something the size of Microsoft Windows, you'll find your alternatives are use headers or kill yourself. – user4581301 Apr 17 '21 at 03:54

1 Answers1

-1

The primary purpose of a header file is to propagate declarations to code files. Header files allow us to put declarations in one location and then import them wherever we need them. This can save a lot of typing in multi-file programs. This program prints “Hello, world!” to the console using std::cout.