OOPortal OOPortal


Dynamic Stack  «Prev  Next»
Lesson 7

Constructing a dynamically sized stack Conclusion

In this module, you explored how to use classes to construct a dynamically sized stack. By closely examining this implementation of a useful data structure, you learned:
  1. How a constructor member function can be overloaded to allow different initializations
  2. How to use a copy constructor
  3. How a destructor member function works


A stack lets you insert and remove elements at one end only, traditionally called the top of the stack. To visualize a stack, think of a stack of books.
New items can be added to the top of the stack. Items are removed from the top of the stack as well. Therefore, they are removed in the order that is opposite from the order in which they have been added, also called last in, first out or LIFO order. For example, if you insert strings "Tom", "Dick", and "Harry" into a stack, and then remove them, then you will first see "Harry", then "Dick", and finally "Tom".
To obtain a stack in the standard C++ library, you use the stack template:

stack <string> s;
s.push("Tom");
s.push("Dick");
s.push("Harry");
while (s.size() > 0){
  cout << s.top() << "\n";
  s.pop();
}