Note that there are some explanatory texts on larger screens.

plurals
  1. PO
    primarykey
    data
    text
    <p>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.</p> <p>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.</p> <p>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.</p> <p>OK - lots of verbiage. A concrete example:</p> <h3>source1.h</h3> <pre><code>extern int somefunc(int, int); </code></pre> <h3>source1.cpp</h3> <pre><code>#include "source1.h" int somefunc(int a, int b) { return a + b; } </code></pre> <h3>program.cpp</h3> <pre><code>#include &lt;iostream&gt; #include "source1.h" using namespace std; int main() { int x = somefunc(1, 3); cout &lt;&lt; "somefunc(1, 3) is " &lt;&lt; x &lt;&lt; endl; } </code></pre> <p>The header here is necessary to provide the assurance that everything is consistent. Both <code>source1.cpp</code> and <code>program.cpp</code> must include the header to ensure the consistency.</p> <p>Note that if <code>somefunc()</code> was defined in <code>program.cpp</code> there would be no point in providing the header.</p> <p>Now suppose we modify <code>somefunc()</code>:</p> <h3>source1.cpp - revised</h3> <pre><code>#include "source1.h" #include &lt;iostream&gt; using namespace std; int somefunc(int a, int b) { cout &lt;&lt; "a = " &lt;&lt; a &lt;&lt; ", b = " &lt;&lt; b &lt;&lt; endl; int x = a + b; cout &lt;&lt; "x = " &lt;&lt; x &lt;&lt; endl; return x; } </code></pre> <p>Superficially, you could revise <code>source1.h</code> so it includes <code>&lt;iostream&gt;</code>, but that would be a bad move. The interface of the function defined, <code>somefunc()</code>, does not depend on <code>&lt;iostream&gt;</code>, so the header should not include <code>&lt;iostream&gt;</code>.</p>
    singulars
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    plurals
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. VO
      singulars
      1. This table or related slice is empty.
    2. VO
      singulars
      1. This table or related slice is empty.
    3. VO
      singulars
      1. This table or related slice is empty.
    1. This table or related slice is empty.
 

Querying!

 
Guidance

SQuiL has stopped working due to an internal error.

If you are curious you may find further information in the browser console, which is accessible through the devtools (F12).

Reload