/* Example programs from the book Scientific and Engineering Programming in C++: An Introduction with Advanced Techniques and Examples, Addison-Wesley, 1994. (c) COPYRIGHT INTERNATIONAL BUSINESS MACHINES CORPORATION 1994. ALL RIGHTS RESERVED. See README file for further details. */ #ifndef CheckedSimpleArrayH #define CheckedSimpleArrayH #include "examples/ch4/SimpleArray.h" template class CheckedSimpleArray : public SimpleArray { public: CheckedSimpleArray(int n); // Create array of n elements CheckedSimpleArray(); // Create array of 0 elements T& operator[](int i); // Checked subscripting CheckedSimpleArray& operator=(const T&); // Scalar assignment class SubscriptRangeError {}; }; // Check for negative size left out on purpose, so inserting it // could be an exercise. template inline CheckedSimpleArray::CheckedSimpleArray(int n) : SimpleArray(n) {} template inline CheckedSimpleArray::CheckedSimpleArray() {} template inline T& CheckedSimpleArray::operator[](int i) { if (i < 0 || i >= numElts()) throw SubscriptRangeError(); return SimpleArray::operator[](i); } template inline CheckedSimpleArray& CheckedSimpleArray::operator=(const T& rhs) { SimpleArray::operator=(rhs); return *this; } #endif