How to distribute classes in .cpp and .h files

J

Javi

I have some doubts about what is the best method to distribute classes in .cpp and .h files:

- Should I use a file per class? or should I group similar classes in one file?
- Is it good to put a lot of class declarations in the same .h, and their definitions in different .cpps?
- And, to the contrary, many definitions in one cpp and declarations in different .h?
- How to deal with precompiled headers? is it ok to include all headers in the precompiled header and include it
everywhere you need?.
- What impact does all this have in the executable size (if any)?
- Is there any document or URL with advice on all these?.

Thank you,

Javier Sanz.
 
E

e-Jah

Javi said:
I have some doubts about what is the best method to distribute classes in .cpp and .h files:

- Should I use a file per class? or should I group similar classes in one file?
- Is it good to put a lot of class declarations in the same .h, and their definitions in different .cpps?
- And, to the contrary, many definitions in one cpp and declarations in different .h?

Personaly I put one class prototype in one .h (myclass.h for ex) and 1
description of 1 class in 1 cpp (myclass.cpp for example)
- How to deal with precompiled headers? is it ok to include all
headers in the precompiled header and include it everywhere you need?.
- What impact does all this have in the executable size (if any)?

Nothing, because when the compiler works, you can imagine it works on
one bigger file whow contains all your files (it's very imaging)
- Is there any document or URL with advice on all these?.

Thank you,

Javier Sanz.

Sorry for my english, if you have the time and if you are brave, don't
hesitate to correct me, thx ;)

Bye
 
H

hari4063

Depends ...

If you map header:class=1:1 :) than other classes can use another ones, so
you must include file(s) in include file (some things are posted recently
about this).

My method is: if classes are not too large and not depend too much on
another clases than i write 1:1 (there is tiny line beatween this).

Execuatble size will not be shorter/bigger if you use any methode, it
depends on implementation of clases.

Grouping all classes in one HUGE header is not good practice, particulary
if you write some stuff to use letter (imagine that all STL are in one
header - it will need a lot of minutes to compile).

All of this is my aprouch, so it will not be the best!

Best,
Zaharije Pasalic
 
E

E. Robert Tisdale

Javi said:
I have some doubts about what is the best method
to distribute classes in source (*.cpp) and header (*.h) files:

- Should I use a file per class?
- Or should I group similar classes in one file?

Your program should be decomposed into *modules*
which consist of compile time class library (header files)
and runtime (static and/or dynamic) class library archives.
The header files describe the module *interface*.
The source files describe the module *implementation*.

It is probably best to decompose modules
into the smallest possible useful units
so that you can re-use the sub-modules in other modules.
- Is it good to put a lot of class declarations in the same header file
and their definitions in different source files?
- And, to the contrary, many definitions in one source file
and declarations in different header files?

Not generally.
Usually, it is better to create separate header files
and include them in other header files to improve "modularity".
- How to deal with precompiled headers?
Is it ok to include all headers in the precompiled header
and include it everywhere you need?

This tends to defeat the purpose of precompiled headers.
- What impact does all this have in the executable size (if any)?
- Is there any document or URL with advice on all these?.

This depends upon your implementation.
Generally, it is better to keep source files
(and the object code files produced from them)
as small as possible so that your link editor
can extract exactly the functions that it needs
from the library archive.
 

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,777
Messages
2,569,604
Members
45,234
Latest member
SkyeWeems

Latest Threads

Top