Lets say I have 3 files: add.h, add.cpp and main.cpp. This is a common example on C++ training sites where add.h contains the prototype of a function called “add”. Add.cpp contains the declaration of the prototype and main.cpp includes add.h and then calls the add() function to add two numbers (x and y) together.
My question is this: No matter what I name add.cpp, my program works just fine. Elephant.cpp works just as well as doctorWhoRules.cpp. Does the compiler search through all the local .cpp files to find the given prototype in add.h? I don’t declare the name of the .cpp file that contains it anywhere. I’m just confused as to how this works.
Thanks!
I think you are mixing
JavawithC++. In Java (editors like Eclipse) there is a general restriction that, yourclassname should same as the file name. i.e.In C++ there are no such restrictions. Your file name can be any meaningful name. The only thing you have to take care is that your implementation files are with
.cpp/.cxx/.Cetc. extensions (I don’t know if modern compilers support other than this also).Even your header file can be any arbitrary extension of course other than
.cppand all; the more conventional extensions for header files are.h,.hppetc.