included .cpp files

My understanding is that when a project written in C++ gets compiled, the program executes the main() function, and then nothing else unless a function is called within main(). Yes, all statements/declarations/methods will be, and must be correctly, compiled even if they aren't used.

In my calculator program, my main.cpp is solely function calls that are defined in other header files (within the project of course). All seems to be working so far, however in my book (Ivor Horton. 2005 C++. Bigger than the last Harry Potter book), at the part where he is creating his own calculator program, he has main.cpp, but also add.h, subtract.h, multiply.h, divide.h, and corresponding .cpp files for each.

Why does he do this? Is it more efficient/memory saving than my way? I don't understand the need for .cpp files other than main.cpp.
This is a way to not have all code ahead your eyes.

Some editors, specially on IDE platforms, allows encryption blocks of lines and if you work with one of these then is more dificult understanding #include directive usefull.

But, enclose many functions to standart .h files or .inc files you can have part of code, error free, to use for more one program.
Building a C++ program consist of two parts. First all source files (i.e. .cpp) are compiled into object files. Second lincer links those files in exetuable. If your program has only one file it will be recompiled every time you make tiny change to the code. If you split your source in several files only those, which were changed will be recompiled. It doesn't matter in small projects, but when you write a ≈100 000 lines programs it will matter.

Also there is readability isuue, possibility to use the same code more than once and others. However having separate add.h, substract.h and others for a simple calculator is a bit overboard.

EDIT: Cataclysm roguelike game have 6 minutes build time on my PC when I build it from scratch. After I intoduced fix into one .cpp file and rebuilt it, it had taken ≈16 seconds.
Last edited on
closed account (zb0S216C)
billywilliams wrote:
"all statements/declarations/methods will be, and must be correctly, compiled even if they aren't used. "

If a compiler knows that a function is never referenced at any point, anywhere, it can remove it so long as it does not change the observable behaviour of the program. This is the general rule the compiler must follow when optimising any code. In addition, code that is never reached can be omitted from the final program, and again, so long as it does not change the observable behaviour of the program.

billywilliam wrote:
"Why does he do this?"

Probably because:-

1) ...it's easier for the reader to comprehend.
2) ...it's easier to maintain the code and project.

billywilliam wrote:
"Is it more efficient/memory saving than my way? I don't understand the need for .cpp files other than main.cpp."

Splitting an implementation into different files helps the programmer to manage the code and project, but is also less efficient on the pre-processing level. I guess you could say that it's a trade-off between pre-processing efficiency and code manageability.

The C++ files (.cpp) are formally known as "translation units" and are also known as "source files" informally; the two are used interchangeably. Source files are compiled, headers are not. Compilers allow a program to be split into different source files which are all linked together during the final stage of program compilation (AKA "linking"). Splitting a program into multiple translation units is optional, but is usually the better practice.

Wazzak
Topic archived. No new replies allowed.