.h and .cpp- as well as classes

I have been writing quite a bit now in the past few days.
I just have two main questions..
first a brief rundown..I keep all my main code in main.cpp..that's the general functionality. However, I have a .h file for all my includes and my personal functions I want to carry around (which I will be re-using in all my C++ apps). Basically...I am about to build an engine to randomly generate a crime scenario out of a list of 2000 something crimes I am building..basically when my game is being ran..at certain points I will call on this class and pass it some variables and it'll automatically randomly pick which one of the crimes, and generate everything for it, and run it.

Basically I have two questions:
1) Should I have classes like that in a .h or .cpp file.
2) If I put them in a .cpp file how do I include a .cpp file..I know how to do the .h files..just not the .cpp.
Separate it. Generally each .h file should have a corresponding .cpp file. For help on how to use .h(pp)/.cpp files, read this:

http://www.cplusplus.com/forum/articles/10627/
to include a .cpp file you create an object file of that class, eg in GNU compiler (mingw and cygwin etc.) you can type on the command line: g++ -c myfile.cpp
this creates a: myfile.o

then to link that to another .cpp that has an int main(): g++ myfile.o myMain.cpp
which creates the exe.

That is under the assumption that there is no main in myfile.cpp otherwise you would get a compiler error. However if it's just a class implementation or functions/structs etc... this works.

If your using an IDE, then typically .o files are in the binary folder along with .exe files or debug/release folder . you simply need to link the .o file to the current .cpp you are compiling.
Last edited on
Topic archived. No new replies allowed.