Hi,
i would like to separate the declaration of a class from the definition.
i know about thevoid aclass::a_method(){ ..... }
but i dont know anything about what should be written in the header file, and how can i use it through main....:confused:
sorry if am not very specific but i don't how to express it...
Can anyone suggest a link for further reading...or maybe explain with an example....:)thanks in advance....
In a header file (generally, a file with an extention of .h or .hpp), the most common approach is to declare classes/functions/etc without providing a definition. Then with a .cpp file of the same name, you provide the definition. eg,
//foo.h
class MyClass
{
int blah;
public:
MyClass() : blah(0) {}
int get_blah();
}
//foo.cpp
#include "foo.h"
int MyClass::get_blah()
{
return blah;
}
You can #include the into your code header the same way as any other library or header file - making sure you provide its path if the header is inside a different directory.
Edit: The above is somewhat incomplete - you should include header guards to stop the file being included twice, ie,
//foo.h
#ifndef FOO_H
#define FOO_H
class MyClass
{
int blah;
public:
MyClass() : blah(0) {}
int get_blah();
}
#endif
The 3 lines highlighted in purple take advantage of the preprocessor, to ensure that everything inbetween the #ifndef and #endif directives can only ever be included once (to include the header file more than once will almost …