Overloading assignment operator for Stack in C++
<p>I am trying to overload the assignment operator for my Stack class (in C++). When I run my output, I get the following:</p>

<p>0 1 2 3 4 5 6 7 
(empty)
Freeing memory!
Freeing memory!</p>

<p>The first line represents Stack a, the second line represents Stack b (see main()) and the "Freeing memory!" is the destructor. For whatever reason Stack a's contents are not copying into Stack b's contents. I have verified that the capacity space is there (did a bunch of print statements) but the copying is not happening. Can someone help?</p>

<p>Here is my Stack constructor, copy constructor, and destructor:</p>

<pre><code>/********************************/
Stack::Stack()
{
    top = -1; // array counter starts at 0
    stk = new int[MAXSIZE];
    capacity = MAXSIZE;
}
/********************************/

/********copy constructor ***********/
Stack::Stack(const Stack&amp; source) {
  stk = new int[source.capacity]; // allocates new array space for the copy constructor
   for (int i = 0; i &lt;= top; i++) {
      stk[i] = source.stk[i];
    }
    top = source.top;
    capacity = source.capacity;
} /******* end copy constructor *******/

Stack::~Stack() {
    cout &lt;&lt; "Freeing memory!" &lt;&lt; endl;
    delete[] stk;
}


Here is my main:


int main() {

    Stack a;
    for (int i=0; i &lt; 8; i++) {
        a.push(i);
    }
    //cout &lt;&lt; "\n Using copying incorrectly...\n";
    //Stack b(a);
    Stack b;
    b = a;
    a.display();
    b.display();
</code></pre>