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 6983625
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 27, 20262026-05-27T18:26:38+00:00 2026-05-27T18:26:38+00:00

Question about header files in C++: when I should use them. If I write

  • 0

Question about header files in C++: when I should use them. If I write small program should I create header file or can I just declare my classes in .cpp files? What is a good practice? If you give links to articles about it, it would be good. I could not find good ones.

  • 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-27T18:26:39+00:00Added an answer on May 27, 2026 at 6:26 pm

    Use header files when you need to share information between several source files. The header file, used carefully, will ensure consistency between a file defining some functionality and other files using that functionality.

    When a program consists of a single (source) file, there is no need to create a header. When a program needs more than one source file (say two source files), there must be at least one function or variable ‘shared’ between the two files (because if there wasn’t, one of the files would be redundant). You should have a header to describe the shared function or variables so that the producer and consumer agree on the common functionality.

    The corollary is that a header should not describe anything not needed by the consumers of the functionality. Just because the implementation needs to use some second header, the consumer should not be made to include it. If the second header provides definitions used in the interface, then it should indeed be included in the common header.

    OK – lots of verbiage. A concrete example:

    source1.h

    extern int somefunc(int, int);
    

    source1.cpp

    #include "source1.h"
    
    int somefunc(int a, int b)
    {
        return a + b;
    }
    

    program.cpp

    #include <iostream>
    #include "source1.h"
    using namespace std;
    
    int main()
    {
        int x = somefunc(1, 3);
        cout << "somefunc(1, 3) is " << x << endl;
    }
    

    The header here is necessary to provide the assurance that everything is consistent. Both source1.cpp and program.cpp must include the header to ensure the consistency.

    Note that if somefunc() was defined in program.cpp there would be no point in providing the header.

    Now suppose we modify somefunc():

    source1.cpp – revised

    #include "source1.h"
    #include <iostream>
    using namespace std;
    
    int somefunc(int a, int b)
    {
        cout << "a = " << a << ", b = " << b << endl;
        int x = a + b;
        cout << "x = " << x << endl;
        return x;
    }
    

    Superficially, you could revise source1.h so it includes <iostream>, but that would be a bad move. The interface of the function defined, somefunc(), does not depend on <iostream>, so the header should not include <iostream>.

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

Sidebar

Related Questions

I have a question about the relationship between header and implementation files in C++.
I have a question about source file and header in c++. I have a
One question about protected constructor. I learnt that the protected constructor can be used
In eclipse, whenever I create a new C++ class, or C header file, I
My question is about when a function should be referenced with the extern keyword
I've been writing a program that can determine any and every data about an
I've got a question about use of permissive-licensed (BSD, Apache, MIT, etc) source where
Sorry about the vague question title, I just want to ascertain some things. Static
Question about Eclipse RCP and whole perspective/view/editor design - what is the best way
Question about subclassing in matlab, under the new class system. I've got class A

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.