I am reviving my C++ knowledge; so to start off with, I have implemented a Stack template. The code for Stack template as well as a sample class to use the stack is given below. It throws a StackException when trying a pop operation on empty stack.
I would like to have suggestions from forum members on any improvements, optimizations, the design or any other aspects.
Don't provide implementations for the copy stuff as in:
1 2 3 4 5
// My Stack should not allow copy of entire stack
Stack(const Stack& lhs);
// My Stack should not allow assignment of one stack to another
Stack& operator=(const Stack& rhs);
The copy constructor and assignment operators are declared private. I'll provide the empty implementation.
I'll try out StackException to derive it from std::runtime_error.
Is there a scope of improvement in the pop method? It calls Student destructor twice - once while deleting topNode and second time when data goes out of scope on returning. The Student object - s3 and s4 will be copy constructed from the Student object returned from pop.
kbw, I have implemented your suggestions and it looks much better now.
Oh... And I just saw that I have provided empty implementation to copy constructor and assignment operator. Did you mean to say not to provide implementation at all? Why is that so? What difference will it make?
I would also like to have some suggestions for the next step - implementing some complex scenario - something related to inheritance, virtual mechanism. Need some real world scenario ideas.