Is it wrong to define member functions within the class itself? Or is it just a matter of code organization to write the prototypes in the class creation and then implement them?
In general it is wrong even.
When it makes sense?
- when you use templates
- when it is a class of constants only
- in very simple programs, such as student programs, in which classes are often defined and
main()
and all in a single source file
Imagine the case of the class vector
, class list
. Of iostream
. These classes redefine several operators for example. And it has many constructs and complex implementation. And you don’t compile it all the time. Only the library is used
If the implementation is all in the header anyone who uses the class will read the implementation and it’s almost never what you want. The implementation may not have been sold, but only the functionality. A lot of people write classes as a source of income ;)
If you have a separate header plus the complete file will have to keep twice the same thing, and this is a risk
If you have multiple implementations of the same class in general you will want to keep a single header with the prototypes for your own safety. And test the implementations in various scenarios knowing that the prototypes and structures are the same