------------------------------------------------------------------------------
MC logo
Bounded Safe Array
[^] CSc 220 Home
------------------------------------------------------------------------------
[Introduction] [Boolean and Control] [Functions] [Arrays and Pointers] [Dynamic Arrays] [Array Errors] [Command Array] [Standard Containers] [Structs and Classes] [Automatic Pointer] [Multi-File Programs] [Copying Objects] [Templates] [Inheritance] [Plain C I/O] [Type Odds And Ends] [Plain C Strings] [File I/O]
[Bounded Safe Array] [Bounded Safe Array Driver] [PrintMe Class] [PrintMe2 Class] [PrintMeLots Class] [PrintMeLots2 Class] [PrintMe Test Driver] [Linked List Node Classes] [Linked List Classes] [Linked List Implementation] [Linked List Driver] [Expression Calculator]
bsafearr.h

#ifndef _BSAFEARR_H_
#define _BSAFEARR_H_

#include <iostream>
#include "safearr.h"
using namespace std;

/* 
 * Bounded safe integer array: Based on the SafeIntArr class in the copying
 * group.  
 */
class BoundedSafeIntArr: public SafeIntArr {
private:
        // Lower bound.  
        int lower;
public:
        // Create a bounded array, with both a lower and upper subscript bound.
        BoundedSafeIntArr(int lb, int ub): SafeIntArr(ub-lb+1) { 
                lower = lb;
        }

        // Convenience constructor when you need the common case of 
        // lower bound zero.
        BoundedSafeIntArr(int size): SafeIntArr(size) { 
                lower = 0; 
        }

        // Wrappers to adapt the base class store and fetch methods.  Bounds
        // checks are correct, though the error messages suffer.  (Could all
        // be fixed if we were using exceptions, but we haven't discussed 
        // those yet.)
        void store(int pos, int tostore) {
                SafeIntArr::store(pos - lower, tostore);
        }
        int fetch(int pos) const {
                return SafeIntArr::fetch(pos - lower);
        }

        // The size() method is inherited and works fine without change.
        // We add these extractors to recover the specific bounds.
        int get_low() const { return lower; }
        int get_hi() const { return lower + size() - 1; }
};

#endif

This is based on the SafeIntArr class from the copying group.