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