title: What exactly goes to a header file and what in implementation file in C++?
body: <p>I have an example with the custom Stack implementation. As far as I understood, the <code>.h</code> files should <strong>only</strong> contain <em>declarations</em> and the <code>cpp</code> files should <strong>only</strong> contain <em>implementations</em>. I have found an example of a custom Stack on the <a href="http://www.cplusplus.com/forum/general/81166/" rel="noreferrer">cplusplus.com/stack_example</a> and it looks something like the following.</p>
  
  <p>Stack.h file</p>
  
  <pre><code>#ifndef _STACK_H_
  #define _STACK_H_
  
  #include &lt;iostream&gt;
  #include "Exception.h"
  
  template &lt;class T&gt;
  class Stack {
      public:
          Stack():top(0) {
              std::cout &lt;&lt; "In Stack constructor" &lt;&lt; std::endl;
          }
          ~Stack() {
              std::cout &lt;&lt; "In Stack destructor" &lt;&lt; std::endl;
              while ( !isEmpty() ) {
                  pop();
              }
              isEmpty();
          }
  
          void push (const T&amp; object);
          T pop();
          const T&amp; topElement();
          bool isEmpty();
  
      private:
          struct StackNode {              // linked list node
              T data;                     // data at this node
              StackNode *next;            // next node in list
  
              // StackNode constructor initializes both fields
              StackNode(const T&amp; newData, StackNode *nextNode)
                  : data(newData), next(nextNode) {}
          };
  
          // My Stack should not allow copy of entire stack
          Stack(const Stack&amp; lhs) {}
  
          // My Stack should not allow assignment of one stack to another
          Stack&amp; operator=(const Stack&amp; rhs) {}
          StackNode *top;                 // top of stack
  
  };
  </code></pre>
  
  <p>Now I have question. This <code>.h</code> file obviously reveals some implementation details. The constructor and the destructor are both <strong>implemented</strong> in the <code>.h</code> file. In my understanding, those should be implemented in <code>.cpp</code> file. Also, there is that <code>struct StackNode</code> which is also <strong>implemented</strong> in the <code>.h</code> file. Is that even possible to implement in the <code>.cpp</code> file and only declare it in the header file? As a general rule, wouldn't it be better if those were in the <code>.cpp</code> implementation file? What would be the best way to code this thing so that it follows C++ rules?</p>