When would you use multiple cpp files.

closed account (2NywAqkS)
I've been learning to program for a few months now in my spare time but I still have not come across a situation where you would need more than just a main.cpp?

also what is the Resource Folder for and how can your use it?
Using just one cpp file gets a big tricky when more than one person is working on the code, or when the codebase runs into the millions of lines of code, or when the project lasts more than a few days and you don't want to have to write out millions of lines of code and then try your first compilation of it.
closed account (2NywAqkS)
Is using another cpp file like a header than? Do you have to #include it?
Visual Studio 2010 doesn't seem to let me include a .cpp.

Like Moschops says, this is good for sharing (even with yourself). You could make a file that can handle all cin extraction (validation and what not), and that way all you have to do is include it for every other project you might work on that needs cin.

I haven't used resource files too much, but they seem to be mostly for graphical programs (icons, the way the mouse looks, registering menu button IDs). Txt's would also be resources, I use VS' editor to look at input/output files instead of having to open notepad. Also if you "Add a new Item" and it is a .txt, it will be created in the proper folder automatically.
Do not #include cpp files.

Each cpp file is compiled separately, by the compiler, into an object file.

Then, each object file is gathered by the linker, which links them into a program or library.

If this is news to you, stop learning whatever you're learning today and learn what the preprocessor/compiler/linker do. Knowing how your text files are turned into a program will help you enormously in the future.
Last edited on
closed account (2NywAqkS)
so how do you include another source file into the main one (main.cpp)?
#include <nameOfFile>

Did you mean "how do you get two separately compiled cpp files into the same program or library?"

If that is what you meant, you tell the linker the name of all the compiled object files you want to put into one executable or library, and it does it.
Last edited on
One nice thing about multiple cpp (source) files is that many compilers will only compile source files that have changed. So if you have 2 million lines of code and you change a && to a || somewhere you only have to compile what is in that file, not an entire 2 million lines. That's significant!
Topic archived. No new replies allowed.