2

Possible Duplicate:
C++ - What should go into an .h file?

I know this is general, but I find it really annoying coding things twice, and would find it a lot easier if I didn't have to browse between two files (my .h and .cc) rather than just keep it all in one.

So, what is the point in having a header file if most of what is there must be rewritten and the rest can just be placed in the .cc.

Instead of:

 class VoterData {
    // raw data
    Voter::States state;
    bool vote;
    unsigned int numBlocked;
    // flags
    bool _hasState, _hasVote, _hasNumBlocked;

  public:
    VoterData();
    void reset();

    // getters
    Voter::States getState();
    bool getVote();
    unsigned int getNumBlocked();
    bool hasState();
    bool hasVote();
    bool hasNumBlocked();
    // setters
    void setState(Voter::States state);
    void setVote(bool vote);
    void setNumBlocked(unsigned int numBlocked);
};

AND:

/* VoterData */
VoterData::VoterData() {
    reset();
}
void VoterData::reset() {
    _hasState = _hasVote = _hasNumBlocked = false;
}

// getters
Voter::States VoterData::getState() { return state; }
bool VoterData::getVote() { return vote; }
unsigned int VoterData::getNumBlocked() { return numBlocked; }
bool VoterData::hasState() { return _hasState; }
bool VoterData::hasVote() { return _hasVote; }
bool VoterData::hasNumBlocked() { return _hasNumBlocked; }
// setters
void VoterData::setState(Voter::States state) {
    this->state = state;
    _hasState = true;
}
void VoterData::setVote(bool vote) {
    this->vote = vote;
    _hasVote = true;
}
void VoterData::setNumBlocked(unsigned int numBlocked) {
    this->numBlocked = numBlocked;
    _hasNumBlocked = true;
}

Why shouldn't I just put it all in the .cc file and declare the class there?

Community
  • 1
  • 1
Garrett
  • 11,451
  • 19
  • 85
  • 126
  • @Joe: its not a duplicate of that, this question asks what are the benefits of header files, while the one you linked asks what should go in the header file and what in the source file. – Daniel Oct 24 '11 at 00:37
  • It should be pointed out that this is not necessary in newer languages which don't depend on textual based inclusion. – Winston Ewert Oct 24 '11 at 00:39
  • Or: http://stackoverflow.com/questions/752793/should-c-eliminate-header-files – Joe Oct 24 '11 at 01:04

6 Answers6

1

I believe there are a lot of reasons, which the most noticeable ones are:

  1. If you put a class in a source file and then include that source file in other source files that use that it will work, but it won't work if anything is outside of the class.
  2. There is a huge speedup if other source files that use the class include only header files and don't have to parse the whole class source file.
  3. Circular references, if class A uses class B and class B uses class A, you have to use header files, its not possible with only source files (there is a possible workaround with templates but its just complicated and will get you into trouble).
Daniel
  • 30,896
  • 18
  • 85
  • 139
  • but what if it is a question for school which is only used by one file? – Garrett Oct 24 '11 at 00:36
  • @Garrett: then I say he should get used to using header files because that whats used in the industry. many things in school are learned although they benefit only in the industry and not in school. – Daniel Oct 24 '11 at 00:40
  • 2
    @Garrett: Not many programmers make their living off of homework assignments. – Benjamin Lindley Oct 24 '11 at 00:42
1

Of course you can put EVERYTHING in a single .cc file and make it compiled & run, no matter how many classes there are. Whether to use header files depends on your purpose and habits. For example, if you want to write a shared library and release it to others, you have to use header files. The users can then include your header files and call the functions you wrote.

Luo
  • 162
  • 1
  • 12
1

C++ doesn't tell you where to put your code. Actually, in your case it might be better to use one file:

struct VoterData {
    // raw data
    Voter::States state;
    bool vote;
    unsigned int numBlocked;
    // flags
    bool _hasState, _hasVote, _hasNumBlocked;
    // No getters, setters and other cruft needed
};

If your class is more complicated than my or your example, you will notice that having a header file gives an advantage in understanding your program.

In that case, the header file (x.h) will be small (e.g. 20 lines of code) and the implementation file (x.cc) will be large (e.g. 200 lines of code - much more than in your example class). Anyone (who wants to understand what the class does, or how to use it) has to look at the 20 lines in the header file and doesn't have to look at the 200 other lines of code - that's great (speaking from experience)!

So, your example doesn't require separation into header and implementation files.

anatolyg
  • 26,506
  • 9
  • 60
  • 134
1

For most any one-off code—which will never be shared with any other programs or persons—then I agree: skip creating a header file and put all the code in a .C, .cc, .c++, etc. file

However, if a class will/should/might be reused—often the goal of much development intent—somewhere along the way it is a great idea to identify the declaration portion and put that in a header file, and the implementation part and put that in a source mode: eventually it may become a shareable library.

wallyk
  • 56,922
  • 16
  • 83
  • 148
0

If everything is in the .cc file and your compiler treats that file as a compilation unit, then you will not be able to use the classes defined in that file in other files (you will get a multiply defined symbol error). Also defining everything in a single file will significantly increase your compilation times, especially in a large project.

Chad
  • 18,706
  • 4
  • 46
  • 63
0

All of the answers so far seem to answer with respect to how the language works according to the specification and current compilers, but I think the question is about why it is designed that way.

When you add a method to a class, you have to put the declaration in two places, wasting programming time. It should be entirely possible to add a compiler pass that generates header files based on their code files, shouldn't it? Slightly longer compile times seems like an okay trade-off nowadays, but it wasn't always so.

One thing the current system does is give you finer control over what is where. There may be some defines for example that you want to have for your code that you don't want to be put into the header for anyone that calls your class.

Jason Goemaat
  • 28,692
  • 15
  • 86
  • 113