< previous page page_1020 next page >

Page 1020
Figure 17-9 depicted exactly the same strategy of keeping a pointer within a class object and letting it point to a dynamic array on the free store.
Specification of the Class: In choosing public operations for the DynArray class, we will retain the three operations from the IntArray class: ValueAt, Store, and a class constructor. The job of the constructor, when it receives a parameter arrSize, will be to allocate a dynamic array of exactly arrSize elementsno more, and no less. Because DynArray class objects point to dynamic data on the free store, we also need a deep copy operation, a class copy-constructor, and a class destructor. Here is the specification of DynArray, complete with preconditions and postconditions for the member functions.
//******************************************************************
// SPECIFICATION FILE (dynarray.h)
// This file gives the specification of an integer array class
// that allows:
//    1. Run-time specification of array size
//    2. Trapping of invalid array indices
//    3. Aggregate copying of one array to another
//    4. Aggregate array initialization (for parameter passage by
//       value, function value return, and initialization in a
//       declaration)
//******************************************************************

class DynArray
{
public:
    int ValueAt( /* in */ int i ) const;
        // Precondition:
        //     i is assigned
        // Postcondition:
        //     IF i >= 0 && i < declared size of array
        //         Function value == value of array element
        //                           at index i
        //     ELSE
        //         Program has halted with error message

    void Store( /* in */ int val,
            /* in */ int i   );
        // Precondition:
        //     val and i are assigned
        // Postcondition:
        //     IF i >= 0 && i < declared size of array
        //         val is stored in array element i

 
< previous page page_1020 next page >