Hi,
I am working on OOD in C++ and have just started to learn about classes and inheritance. So far I could follow the tutorial and my programs have compiled and have executed as expected.
I have 2 classes. Class baseClass and class derivedClass that is a public inheritance from baseClass. Previously I had both class declarations in the same header file (classes.h) and both implementations in the same cpp file (implementation.cpp). The program compiled, no problems.
Now I want to split the code, i.e. each class should have its own header and its own implementation.cpp. My question is, how do I handle all this with the header includes, as I keep getting errors that my base class gets "redefined". My research so far implies that I should use
1 2 3 4
|
#ifndef ????
#define ????
#endif
|
to prevent that headers get included twice.
Can someone please explain me how to include and link these files properly?
Here's my current settings. All files are part of the same project. In Dev-cpp I don't need to link the implementation files anywhere, as the compiler does so automatically, at least it had worked previously when I had only 1 class (1 header + 1 implementation.cpp + main.cpp).
baseclass.h
- no includes
- contains class declarations
baseclass.cpp
- #include "baseclass.h"
- contains base class implementation
derivedclass.h
- #include "baseclass.h"
- contains class derived: public base
derivedclass.cpp
- #include "derivedclass.h"
- contains derived class implementation
main.cpp
- #include "baseclass.h"
- #include "derivedclass.h"
- contains main()
Again, all I did is separate the base and derived class into saperate header/implementation files and it stopped working. In my tutorial the section following is very briefly talking about how to avoid including a header twice (which seems to be the problem), but I don't quite understand how to do this, and where to put the code section quoted above.
Thanks a lot in advance. Help is greatly appreciated! :)