I'm new to cpp, say I want to use an object Dog, is it ok to use only header file for the class Dog? the methods would be implemented in an inline manner.
Or is it that I should partition the class to an header and a cpp file implementing the methods defined in the header file?
The class Dog is used in some other cpp file (as a concrete object and not a pointer - Dog dog = new Dog()...).
Asked
Active
Viewed 51 times
2

TomM12
- 55
- 1
- 8
-
The last sentence lent little to your question, as it is wrong (it should just read `Dog dog;` with no `new` invoke). Regardless, your question is one largely of circumstance mixed with personal preference. there are equally valid reasons for *either* choice, and even a mix of *both*. It isn't a one-size-fits-all shoe. – WhozCraig Apr 19 '16 at 07:13
-
Do whatever you want, it is okay to implement in '.h' file for small classes. You could use '.hpp' extensions to be more explicit. – MokaT Apr 19 '16 at 07:15
-
It is okay to have implementation in the header file. But be careful with [circular dependencies](http://stackoverflow.com/q/625799/3344612) – Teivaz Apr 19 '16 at 07:32
1 Answers
1
is it ok to use only header file for the class Dog?
Yes, it is ok.
But when programs are getting larger, it's more practical to separate declaration from implementation and organize the program into semi-independent code fragments, which can be used to minimize compilation times and chance of errors.

Andreas DM
- 10,685
- 6
- 35
- 62