>right now i have included "idr_bpl.cp p", "idr.h" and other standard
files (like iostream etc........) in bpl.cpp and
Bad idea. cpp files are implementation files, which are not meant to
be included.
The whole idea behind this is quite simple. You must understand the
diference between a compiler and a linker. There are very good books
out there, check out
www.accu.org.
But let's put a simple example :
int main()
{
}
Quite simple. Now you have function which you want to use :
void f()
{
std::cout << "this is my function";
}
So you put it before main and you call it :
void f()
{
std::cout << "this is my function";
}
int main()
{
f();
}
Now, your project is getting larger and you want to seperate the
main() from other functions. You keep main() in your original file
(let's call it main.cpp) and you put your function in a new one (let's
call it function.cpp). Your main.cpp now won't even compile, since
you put f() in another file. You have to tell your compiler that f()
exists, but somewhere else :
// main.cpp
void f();
int main()
{
f();
}
And that's it. The compiler parses both files and finds no errors.
Then the linker kicks in, puts the two files together and the call to
f() in main() is 'linked' to the f() in function.cpp.
But now, let's get on with a new project :
// myclass.h
class MyClass
{
private:
int some_stuff;
public:
MyClass(int s) : some_stuff(s)
{
}
int stuff()
{
return some_stuff;
}
};
This is your class and it is in a header file ("myclass.h" ). This
header is meant to be included by any file which uses this class :
// main.cpp
# include "myclass.h"
int main()
{
MyClass my_object(5);
}
But now, imagine you have some twenty different files which are all
including "myclass.h" . Everything goes pretty well until you decide
the constructor must validate the int before putting it in
'some_stuff'. You change some code and you recompile. Since the
header changed, all of your twenty files must be recompiled too.
This is the reason for which you have to separate the class definition
and the class' implementation. In your header file, you will only put
the function's declarations :
// myclass.h
class MyClass
{
private:
int some_stuff;
public:
MyClass(int s);
int stuff();
};
That's it. Now your twenty files are quite happy with that since they
only need declarations, not definitions of member functions.
And since you have to put the implementation somewhere, you get
another file which you call "myclass.cp p" which will contain the
implementations :
// myclass.cpp
# include "myclass.h"
MyClass::MyClas s(int s) : some_stuff(s)
{
}
int MyClass::stuff( )
{
return some_stuff;
}
So if you have to change something in the code, the only file which
will have to get recompiled is "myclass.cp p". Of course, if you want
to add another member function (such as
void stuff (int new_stuff);
), you will change the header file and the whole thing will recompile.
But that is normal.
So that is the deal : class definitions go in .h files and class
implementation go in .cpp files. You include the .h file when you
need that class, but you never include the .cpp file.
And usually, you will have no more than a couple of classes in a
header file.
Jonathan