Logo Data Structures and Algorithms with Object-Oriented Design Patterns in C++
next up previous contents index

Constructor and Destructor

The definitions of the StackAsArray class constructor and destructor are given in Program gif. The constructor takes a single parameter, size, which specifies the maximum number of items that can be stored in the stack. The variable array is initialized to be an array of length size. Its constructor requires O(1) time to construct an array of pointers. Therefore, the total running time for the StackAsArray constructor is O(1).

   program5841
Program: StackAsArray Class Constructor, Destructor and Purge Member Function Definitions

The StackAsArray destructor simply calls the Purge member function. The behavior of the Purge function depends on whether the stack (as a container) is the owner of the contained objects. If it is the owner, then the contained objects must be deleted by the Purge function. In this case, the Purge function deletes one-by-one the first count elements of the array. In general, because of the polymorphic implementation of Objects, we cannot know the running time to delete the array elements. However, if we assume that the destructors for all the objects each run in constant time, the total running time for the Purge function is O(n), where tex2html_wrap_inline61308, i.e., n is the number of elements in the stack.


next up previous contents index

Bruno Copyright © 1997 by Bruno R. Preiss, P.Eng. All rights reserved.