#include const int defaultSize = 10; class Array { public: Array(int size = defaultSize); Array(const Array & rhs); ~Array(); Array & operator=(const Array &); int & operator[](int index); const int & operator[](int index) const; int getSize() const; friend std::ostream & operator<<(std::ostream &, const Array &); class xLimits {}; class xSize {}; class xTooBig: public xSize{}; class xTooSmall: public xSize{}; class xNull:public xTooSmall {}; class xNegative: public xSize{}; private: int * pType; int pSize; }; Array::Array(int size) : pSize(size) { if (size < 0) throw Array::xNegative(); if (size == 0) throw Array::xNull(); if (size < 10) throw Array::xTooSmall(); if (size > 999) throw Array::xTooBig(); pType = new int[size]; for (int i = 0; i < size; i++) pType[i] = 0; }; Array::Array(const Array & rhs) { pSize = rhs.getSize(); pType = new int[pSize]; for (int i = 0; i < pSize; i++) pType[i] = rhs[i]; }; Array::~Array() { delete[] pType; }; Array & Array::operator=(const Array & rhs) { if (this == &rhs) return *this; delete[] pType; pSize = rhs.getSize(); pType = new int[pSize]; for (int i = 0; i < pSize; i++) pType[i] = rhs[i]; return *this; } int & Array::operator[](int index) { int size = Array::getSize(); if (index >= 0 && index < size) return pType[index]; throw Array::xLimits(); return pType[0]; }; const int & Array::operator[](int index) const { int size = Array::getSize(); if (index >= 0 && index < size) return pType[index]; throw Array::xLimits(); return pType[0]; //for MFC }; int Array::getSize() const { return pSize; }; std::ostream & operator<< (std::ostream & out, const Array & myArray) { for (int i = 0; i < myArray.getSize(); i++) out << "[" << i << "] " << myArray[i] << std::endl; return out; }; int main() { try { Array arrInt(-1); for (int j = 0; j < 100; j++) { arrInt[j] = j; std::cout << "arrInt[" << j << "] OK.." << std::endl; } } catch (Array::xLimits) { std::cout << "Unable to process your query!" << std::endl; } catch (Array::xTooBig) { std::cout << "This array is too big!" << std::endl; } catch (Array::xTooSmall) { std::cout << "This array is too small!" << std::endl; } catch (Array::xNull) { std::cout << "You requested an array with no object!" << std::endl; } catch (...) { std::cout << "An issue occurred!" << std::endl; } std::cout << "Completed!"; system("pause"); return 0; }