title: C++ Copy Constructor Linked Stack
body: <p>so I have a copy constructor of a class over here...</p>
  
  <pre><code>template&lt;class ItemType&gt;
  LinkedStack&lt;ItemType&gt;::LinkedStack(const LinkedStack&lt;ItemType&gt;&amp; aStack)
  {
  // Point to nodes in original chain
  Node&lt;ItemType&gt;* origChainPtr = aStack.topPtr;
  
  if (origChainPtr == nullptr)
      topPtr = nullptr;  // Original stack is empty
  else
  {
      // Copy first node
      topPtr = new Node&lt;ItemType&gt;();
      topPtr-&gt;setItem(origChainPtr-&gt;getItem());
  
      // Point to last node in new chain
      Node&lt;ItemType&gt;* newChainPtr = topPtr;
  
      // Advance original-chain pointer
      origChainPtr = origChainPtr-&gt;getNext();
  
      // Copy remaining nodes
      while (origChainPtr != nullptr)
      {
          // Get next item from original chain
          ItemType nextItem = origChainPtr-&gt;getItem();
  
          // Create a new node containing the next item
          Node&lt;ItemType&gt;* newNodePtr = new Node&lt;ItemType&gt;(nextItem);
  
          // Link new node to end of new chain
          newChainPtr-&gt;setNext(newNodePtr);
  
          // Advance pointer to new last node
          newChainPtr = newChainPtr-&gt;getNext();
  
          // Advance original-chain pointer
          origChainPtr = origChainPtr-&gt;getNext();
      }  // end while
  
      newChainPtr-&gt;setNext(nullptr);           // Flag end of chain
  }  // end if
  }  // end copy constructor
  </code></pre>
  
  <p>And I did a simple program but my professor told me that I didn't use the copy constructor and I don't seem to find how to do it. All I got is this but it doesn't display the second object "lStack2".</p>
  
  <p>Here is my main...</p>
  
  <pre><code>#include &lt;iostream&gt;
  #include "LinkedStack.h"
  using namespace std;
  
  int main(){
  
  int val1, val2, val3, val4;
  LinkedStack&lt;int&gt; lStack1;
  
  LinkedStack&lt;int&gt; lStack2;
  
  lStack2 = lStack1;
  
  cout &lt;&lt; "Enter the first value: ";
  cin &gt;&gt; val1;
  
  cout &lt;&lt; "Enter the second value: ";
  cin &gt;&gt; val2;
  
  cout &lt;&lt; "Enter the third value: ";
  cin &gt;&gt; val3;
  
  cout &lt;&lt; "Enter the fourth value: ";
  cin &gt;&gt; val4;
  
  lStack1.show();
  lStack2.show();
  
  return 0;
  }
  </code></pre>
  
  <p>And here is my display function...</p>
  
  <pre><code>template &lt;class ItemType&gt;
  void LinkedStack&lt;ItemType&gt;::show() const{
  
  Node&lt;ItemType&gt;* entryPtr = topPtr;
  while (entryPtr != nullptr){
      cout &lt;&lt; entryPtr-&gt;getItem() &lt;&lt; endl;
      entryPtr = entryPtr-&gt;getNext();
  }
  }
  </code></pre>
  
  <p>Can someone help to figure out how to implement the copy constructor?</p>