0

Separating class code into a header and cpp file answers how to declare and define classes using headers and sources; Why can templates only be implemented in the header file? answers why (when you are going to) declaration and definition of a template-functions are done in the header. My question is specific about a functions-only header; specifically about when and why to use them, instead of how to use them.

I am learning how to use headers and multiple source files. I just created a header which I called "ival.hpp" where I implement some template-functions to validate input: test if they match desired types, ask for input of specific types, ask for [y/n] validation... Just very basic things for my level (only functions, no classes in that header).

This brings the question: What is the convention for using headers? Should the main.cpp only contain the main() function while everything else is categorized in headers to simplify readability? Are headers only used to define classes (and their functions) by convention? In future projects would it be reasonable to #include "ival.hpp" instead of just declaring and defining them in the main.cpp?

Daniel Duque
  • 137
  • 9
  • I think first you should know that C and C++ are different languages with different paradigms (also related to the headers/sources separation) – Eugene Sh. Feb 11 '19 at 19:34
  • Conventions vary to some extent between C and C++. You seem to be asking about C++, so I have edited your tags appropriately. – John Bollinger Feb 11 '19 at 19:36
  • Side note: [Why can templates only be implemented in the header file?](https://stackoverflow.com/questions/495021/why-can-templates-only-be-implemented-in-the-header-file) – user4581301 Feb 11 '19 at 19:43
  • @πάντα ῥεῖ there may be dupe of this somewhere, but the one you dug out is not a dupe, as it is about `class` implementations, while this question is specifically about functions. – Walter Feb 11 '19 at 20:13

0 Answers0