I don't know about Java, but in C++ you compile the source into object files and then you link the object files into the executable. If you're using an IDE then these two steps will probably be obscured and you will simply click "build" or something like that.
Anyway, to answer the question, you should just have the names of the header files of the classes that you want to use in the cpp files of the files that you want to use them. So, say I have class A and another class B . Both A and B might have their own .h and .cpp files. Your main function might be in another file on its own, but you will use classes A and B in main . So, A.h might look like:
/* Some "standard" headers */
/* Header file for a class used in this class */
/* Forward declaration (we can do this if you */
/* only use a pointer to the class, as below) */
// Stuff inside class A
/* Class A has member variables that are other classes */
C m_C; // Can't forward declare for this
D* m_pD; // this type is forward declared (which helps compilation time on a large project)
and it's .cpp file would look like:
/* Only need to include the header file for this class here */
/* since the others are all taken care of in A.h, except for */
/* any forward declared classes (like D, in this case) */
/* Need to include the header files for any forward declared classes */
// Define A's functions here
Class B would be similar. Now, main would have to include the headers for the classes that are used:
// Do things using classes A and B here
// Can't do anything with C or D unless you also include them above,
// but A and B will still compile, because C & D are included in them
Now, the compilation and linking would look something like this (using gcc): g++ main.cpp A.cpp B.cpp C.cpp D.cpp -o a.exe So, the main file and all the class cpp's are in the compilation. The compiler figures out what where to get the headers from by looking in the cpp's. The -o a.exe says that the object files produced by the compiler should be linked into an executable called "a.exe".
It surely is legal. In fact, you need to have exactly one function named main() when you build an executable. So, if you have a main function in several cpp files, you are in trouble. Usually, cpp files which are associated with a class or a set of utility functions that you wrote, will not have a main function (unless it is a unit-test harness that is conditionally compiled only).
So, in order to be able to make an executable, the compiler will compile all those cpp files independently (called translation units) into object files (.o or .obj) and then put them all together (called linking). At the end of this process, it basically has one big chunk of compiled code linked together. It then looks through that chunk of code of a main() function to use as the "entry-point" of the executable. If it does not find one and exactly one main() function, it will give an error.
The typical setup is to have the main function in its own cpp file and do as ravenous laid out, compile a list of cpp files that includes the "library" cpp files and the one "application" cpp file (main.cpp or something like that).
For unit-testing, it is typical to add a main function at the end of each cpp file that is a simple test program that calls and tests every function in your cpp file. But, in this case, you have to enclose it in a #ifdef #endif for conditional compilation, such that they don't interfere with the compilation of applications.