Help me about Header Files

D

dharmesh Gupta

Hi All,

i am new to header files , and have a basic question ,
i have made my program in three files namely,

idr_bpl.cpp( contains class definitions and function definitions)
bpl.cpp( contains main() function)
bpl.h (contains variable definition of a variable that is shared in
the above two files )

right now i have included "idr_bpl.cpp", "idr.h" and other standard
files (like iostream etc........) in bpl.cpp and

"idr.h" and other standard files in idr_bpl.cpp

now, as suggested by one google friend that it is not a good practice
to include a cpp file in another cpp file , i want to make header
files for separating the definitions and implementation,
the point to be noted is that there are 2 classes(namely body and
header) defined in idr_bpl.cpp and the second class's(header's) one
function uses the object of the other class(body).

can anyone help me arranging my code in proper header files, since i
will have to make several such programs in future.

Thanks
Dharmesh
 
J

John Harrison

dharmesh Gupta said:
Hi All,

i am new to header files , and have a basic question ,
i have made my program in three files namely,

idr_bpl.cpp( contains class definitions and function definitions)
bpl.cpp( contains main() function)
bpl.h (contains variable definition of a variable that is shared in
the above two files )

right now i have included "idr_bpl.cpp", "idr.h" and other standard
files (like iostream etc........) in bpl.cpp and

"idr.h" and other standard files in idr_bpl.cpp

now, as suggested by one google friend that it is not a good practice
to include a cpp file in another cpp file ,
Right.

i want to make header
files for separating the definitions and implementation,
the point to be noted is that there are 2 classes(namely body and
header) defined in idr_bpl.cpp and the second class's(header's) one
function uses the object of the other class(body).

can anyone help me arranging my code in proper header files, since i
will have to make several such programs in future.

Thanks
Dharmesh

Just put the classes in the header file, but put the class functions in the
cpp file.

// idr.h

class X
{
public:
void a_func();
};

class Y
{
public:
void b_func();
};

// idr_bpl.cpp

void X::a_func()
{
Y y;
y.b_func();
}

void Y::b_func()
{
}

john
 
J

Jonathan Mcdougall

right now i have included "idr_bpl.cpp", "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.cpp" which will contain the
implementations :

// myclass.cpp

# include "myclass.h"

MyClass::MyClass(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.cpp". 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
 
P

puppet_sock

[email protected] (dharmesh Gupta) wrote in message news: said:
can anyone help me arranging my code in proper header files, since i
will have to make several such programs in future.

Others have already given you some good advice. Here's some
little "tricks" to help you get into this in the right way.

CAVEAT! None of these rules are carved in stone. There are cases
where breaking them is better than following them.

The header file for a class should contain exactly the information
required to use that class. No more and no less. The idea is, a
client of the class needs to include the header, and it gets all
it needs out of there. It should not require that other headers
be included to use the class. The header is like a contract for
using the class.

To encourage that, follow this rule. In the implementation file of
a class, make the first line the #include for the header for that
class. Then, as you build, anything that will be needed by a client
goes in the header. Anything that is only needed by the class gets
included in the implementation file with extra #include lines
that appear *after* the include for the header file. So, it's a
careful sort into "needed by client" and "not needed by client."

Use double-include guards. Look up and get to know the #define
and #ifdef compiler directives. That way, you can safely inlcude
standard header files in your class header files.

Socks
 
M

Michael Spencer

can anyone help me arranging my code in proper header files, since i
will have to make several such programs in future.

You can try Lazy C++. Write your component in one file, define
entities at their declaration (Java-style), then run lzz, a C++
preparser. Many onerous coding tasks go away, like writing header
include guards and removing default args from definitions. Specially
comes in handy when you want your template definitions in a separate
file (if you want to explicitly instantiate your templates) or
inline function definitions in a separate file (for conditional inline
compilation).

You can try it out online at

http://www.lazycplusplus.net/cgi-bin/lzzcgi

It's also at

http://www.lazycplusplus.com

Hope this helps,

Mike
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

No members online now.

Forum statistics

Threads
473,744
Messages
2,569,484
Members
44,903
Latest member
orderPeak8CBDGummies

Latest Threads

Top