Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • SEARCH
  • Home
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 6747247
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 26, 20262026-05-26T12:25:09+00:00 2026-05-26T12:25:09+00:00

Possible Duplicate: C++ – What should go into an .h file? I know this

  • 0

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?

  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-05-26T12:25:09+00:00Added an answer on May 26, 2026 at 12:25 pm

    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.

    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

Possible Duplicate: Reading through file using ifstream I'm trying to find a way to
Possible Duplicate: git - removing a file from source control (but not from the
Possible Duplicate: Why not use tables for layout in HTML? Under what conditions should
Possible Duplicate: How do I calculate someone's age in C#? Maybe this could be
Possible Duplicate: Singleton: How should it be used Following on from Ewan Makepeace 's
Possible Duplicate: How can I combine multiple rows into a comma-delimited list in Oracle?
Possible Duplicate: php == vs === operator Reference - What does this symbol mean
Possible Duplicate: Learning to write a compiler I looked around trying to find out
Possible Duplicate: What is 0x10 in decimal? I notice that Console.WriteLine(18); writes 18, but
Possible Duplicate: Where can I find a web-project “security checklist?” i was just wondering

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.