0

I know that header files are used for declaration of functions and classes, and cpp files are where you give them their definition, but why do this when you can just declare and define a function within the cpp file? Also if anyone has any good resources on goo C++ practices that'd be great.

  • 2
    The reason is that you want to share the same declaration among multiple C/C++ files. – skyking Jul 21 '16 at 12:05
  • 1
    Think about what happens if you have more than one cpp file in your project and one file wants to call a function in the other file. You have to declare the function in one file, and make sure it matches the definition in the other file. Now think about what happens if you have ten .cpp files, or 100, and you declare the function in all of them, then you change the definition to change the return type. Do you want to fix the other nine (or 99) files, or would you rather have the declaration in one header file they all share? – Jonathan Wakely Jul 21 '16 at 12:06

0 Answers0