C++ Stack using template I'm learning C++, so I wrote a stack with the help of templates. I'm wondering if there is anything I can improve or if I did something vastly wrong.

Stack.h

#pragma once
#include &lt;ostream&gt;

template &lt;class Type&gt;
struct Node {
    Node(Type data, Node&lt;Type&gt;* next) 
        : data(data), next(next) {}
    Node* next;
    Type data;
};

template &lt;class Type&gt;
class Stack
{
public:

    Stack() : length(0), topNode(NULL) {
    }

    ~Stack() {
        while (!isEmpty()) {
            pop();
        }
    }

    void push(Type data) {
        Node&lt;Type&gt;* newNode = new Node&lt;Type&gt;(data, topNode);
        topNode = newNode;
        ++length;
    }

    Type pop() {
        if (!isEmpty()) {
            Node&lt;Type&gt;* popped = topNode;
            Type poppedData = popped-&gt;data;
            topNode = popped-&gt;next;
            --length;
            delete popped;
            return poppedData;
        }

        throw new std::exception("Stack underflow!");
    }

    bool isEmpty() {
        return length == 0;
    }

    void print() const {
        Node&lt;Type&gt;* tempTop = topNode;
        while (tempTop != NULL) {
            std::cout &lt;&lt; tempTop-&gt;data &lt;&lt; endl;
            tempTop = tempTop-&gt;next;
        }
    }

    int count() const {
        return length;
    }

private:
    Node&lt;Type&gt;*         topNode;
    int                 length;

};


Main.cpp

#include &lt;iostream&gt;
#include "Stack.h"

using namespace std;

int main() {
    Stack&lt;int&gt; myStack;

    // Push some values
    myStack.push(2);
    myStack.push(4);
    myStack.push(8);
    myStack.push(16);
    myStack.push(32);

    // We pop the 32
    myStack.pop(); 

    // Display count after the pop
    int lastPopped = myStack.pop();
    cout &lt;&lt; "Popped value: " &lt;&lt; lastPopped &lt;&lt; ", Count: " &lt;&lt; myStack.count() &lt;&lt; endl; 

    // Print whole stack
    cout &lt;&lt; endl &lt;&lt; "Stack print: " &lt;&lt; endl;
    myStack.print(); 

    // Exit program when the 'any' key is pressed.
    system("PAUSE");
    return 0;
}
If you provide a destructor you should handle copying and assigning. In other words follow the Rule of 3 (or 5 if you care about move semantics). You can also disallow them but then there should be move constructor and assignment.

Otherwise you will get in trouble with double freeing when calling a function void foo(Stack&lt;int&gt; s).



empty() doesn't change the stack. So it should also be const.

Consider adding a peek() method that returns a reference to the top value of the stack (with const and non-const version).

T&amp; peek()
{
    return topNode-&gt;data;
}

const T&amp; peek() const
{
    return topNode-&gt;data;
}


The reason for using the reference is to avoid a unnecessary copy. 



Having a print method interacting with the console is not a good idea. What if you want to show the contents in a gui instead?

Instead add iterators to be able to inspect what is on the stack without having to capture the output that print() generates.