Question

I am using Visual C++ 2008 Express for the first time for a project. And I can't seem to be able to split the .h & .cpp files for classes I'm writing. I was under the impression that you add a header file and prototype the class in there, and then you add a .cpp file with the implementation into your source files directory. Then when you include the .h it would automatically include the .cpp implementation files. Is this correct or am I missing something?

Was it helpful?

Solution

Not sure if this is the same as in Express version. But you can also add a new C++ class with header (.h) and source (.cpp) at the same time by right clicking on the project -> Add -> Class...

By including the .h file using #include, doesn't mean the actual implementation (in another .cpp file) is also include in your source file. The content of .h file which are class and method prototypes is only included. These prototypes allow you to make use of the classes declared in header file (without including real C++ code.)

Each source files (.cpp) are first compiled into object files. All these object file are then linked together to create single executable file. The referenced symbols in, each object file, are actually linked to their implementation during this linking process (http://www.cprogramming.com/compilingandlinking.html)

OTHER TIPS

I don't remember the rules, but sometimes the IDE assumes you're putting all your code in the header file. This is legal, but not a common preference.

Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top