From what I can understand, it seems, by convention, a class would be defined in a .h file and implemented in a .cpp file. The programs that would use these classes would have to #include the .h file.
1) is this correct?
2) does the class.h have to #include class.cpp?
and finally, I know a whole class can be implemented in a .h file, so
3) why are classes written and implemented seperately?
Thanks in advance, this is my first post on this site.
Regarding 3:
Perhaps this is off the point, but also if you implement class member functions inside the class, they are counted as inline, which is not good for larger methods.
The only difference between defining a class member function completely within its class or to include only the prototype and later its definition, is that in the first case the function will automatically be considered an inline member function by the compiler, while in the second it will be a normal (not-inline) class member function, which in fact supposes no difference in behavior.
7.1.2.3. A function defined within a class definition is an inline function. [...]
@Xander314
Just because a function is inline, doesn't mean calls to it are actually getting inlined.
That's entirely up to the compiler, which will inline only in situations where it thinks it's a good idea (even if a function is not declared inline).
@Athar I was aware that the compiler the final say over this feature. I didn't know it could inline functions not marked as inline. Doesn't that make inline completely redundant?
Yes, pretty much.
It's still needed when defining non-template functions in headers to avoid multiple definitions, but aside from that the compiler just does what it wants anyway (which is a good thing, of course).
The only difference between defining a class member function completely within its class or to include only the prototype and later its definition, is that in the first case the function will automatically be considered an inline member function by the compiler, while in the second it will be a normal (not-inline) class member function, which in fact supposes no difference in behavior.
as i thought too, i guess there could be no difference between defining the function member within a class or outside the class. it should have the same effect...
@question number 2: it's reversed. the .cpp file include the .h file...
@Athar Does that even include functions in static libraries (i.e. that have already been compiled)?. I think I read somewhere that modern optimizing compilers do tend to talk to the linker these days...
Traditionally, the compiler needs to be able to see the definition of the function to be inlined, which is usually only the case when it is defined in a header.
But you're right, modern compilers support link time optimization, so they are able to inline a function from a static library if the static library was compiled with LTO enabled and you compile and link your program with LTO enabled as well.
What about if it's defined in one of the source files of the project? Could the compiler choose to inline that before compiling it, or would it only be possible at link time via LTO?
Since each translation unit is compiled independently from each other, the compiler can't see function definitions in other translation units at compile-time, so the inlining won't happen at that stage.
By its nature, the linker always knows which translation units the program consists of and so this allows the sort of inter-procedural optimizations that the compiler wasn't able to do.