multiple .cpp files

I post this at the risk of asking a question too wide in scope, but here goes. I want to know in what circumstances would someone want to have more than one .cpp file in a project, and how the compiler treats them.I know they can share headers but I don't understand how multiple .cpp files relate (programmatically).

Thanks in advance.
closed account (48T7M4Gy)
This comprehensively addresses your question with a fair bit of diversity
http://www.cplusplus.com/forum/general/39618/
There was a fair bit of diversity in that link, but I might attempt to explain very quickly.

In C++ the major use of cpp and header files is for classes. Class definitions ( class MyClass {}; ) go in a header file, I like *.hpp because it gives a clue that there is cpp code inside, as opposed to C code. The definition of the class functions ( double MyClass::Myfunction() {} )go in a *.cpp file. The header and cpp file should be named exactly after the class name. A *.cpp file should include the header file for it's class definition, and any other header file it needs - #include <string> say.

With C style coding, one can have header files with declarations of functions, enum, struct etc. The functions can have the extern keyword. extern means that the function definition is in another file somewhere. All the *.cpp files are compiled, the linker sorts out where definitions of functions are.

Have a look in the tutorial about classes :+)
Topic archived. No new replies allowed.