#include
using namespace std;
const int DefaultSize = 10;
class xBoundary {};
template
class Array
{
public:
Array(int itsSize = DefaultSize);
Array(const Array &rhs);
~Array() { delete [] pType;}
Array& operator=(const Array&);
T& operator[](int offSet);
const T& operator[](int offSet) const;
int GetitsSize() const { return itsSize; }
friend ostream& operator<< (ostream&, const Array&);
class xSize {};
private:
int *pType;
int itsSize;
};
template
Array::Array(int size):
itsSize(size)
{
if (size <10 || size > 30000)
throw xSize();
pType = new T[size];
for (int i = 0; i pType[i] = 0;
}
template
Array& Array::operator=(const Array &rhs)
{
if (this == &rhs)
return *this;
delete [] pType;
itsSize = rhs.GetitsSize();
pType = new T[itsSize];
for (int i = 0; i pType[i] = rhs[i];
}
template
Array::Array(const Array &rhs)
{
itsSize = rhs.GetitsSize();
pType = new T[itsSize];
for (int i = 0; i pType[i] = rhs[i];
}
template
T& Array::operator[](int offSet)
{
int size = GetitsSize();
if (offSet >= 0 && offSet < GetitsSize())
return pType[offSet];
throw xBoundary();
return pType[0];
}
template
const T& Array::operator[](int offSet) const
{
int mysize = GetitsSize();
if (offSet >= 0 && offSet < GetitsSize())
return pType[offSet];
throw xBoundary();
}
template
ostream& operator<< (ostream& output, const Array& theArray)
{
for (int i = 0; i output << "[" << i << "] " << theArray[i] << endl;
return output;
}
int main(){
try
{
Array intArray(9);
for (int j = 0; j< 100; j++)
{
intArray[j] = j;
cout << "intArray[" << j << "] okay..." << endl;
}
}
catch (xBoundary)
{
cout << "Unable to process your input!\n";
}
catch (Array::xSize)
{
cout << "Bad Size!\n";
}
cout << "Done.\n";
return 0;
}