Hello everyone, I was wondering if there is a way to put multiple classes (".h" & ".cpp") in to one file? I would like to be able to use this file in my projects so I don't have to keep importing all of the .h and .cpp files each project.
If they are your classes, then yes just put the class prototypes in the header file and the definitions in the cpp file. If it is a library classes then you would have to make your own wrappers for them and link with the library. Now if you mean make it header files and cpp files into just one file itself then you would have to put the full class definitions in a cpp file and sit the cpp file in your project (if I'm understanding the question). Though I may have just made the answer majorly confusing. Though it is considered good programming habit to have a header and cpp file.
To clarify:
HEADER:
1 2 3 4 5 6 7
class head
{
public:
// function prototypes
private:
// private data types
};
CPP FILE:
1 2
#include "header"
void head::function();
SINGLE FILE (cpp):
1 2 3 4 5 6 7
class head
{
// same as above
};
void head::func();
// rest of class definitions
Well as the programmer it is entirely your choice as to which method you use. It is considered a good programming habit though to have both files in the project because if you did turn them into a library it becomes encapsulation. If you only use it in your projects it doesn't matter, but if you distribute it then you would only need the library and the header files (which would keep the C++ file private so they don't know your algorithms or anything about your implementation code).