#ifndef CLASS_ARRAY
#define CLASS_ARRAY
#include<iostream>
#include<cstdlib>
using namespace std;
enum Error
{
Invalid_Size,Allocation_Error,Out_Of_Bound
};
enum Mode
{
Change,Append
};
char *ErrorMsg[]={"Invalid Array Size","Memory Allocation Error","Index Out Of Bound"};
template<class T>
class Array
{
private:
T *content;
int size;
void ErrorExit(Error type);
public:
Array(int n=10);
Array(Array<T> &source);
~Array();
Array<T>& operator =(Array<T> &source);
T& operator [](int index);
operator T*();
int PeekSize();
void Resize(int n,Mode mode=Change);
};
template<class T>
void Array<T>::ErrorExit(Error type)
{
std::cout<<ErrorMsg[type]<<endl;
exit(0);
}
template<class T>
Array<T>::Array(int n)
{
if(n<0) ErrorExit(Invalid_Size);
content=new T[n];
if(content==NULL) ErrorExit(Allocation_Error);
size=n;
}
template<class T>
Array<T>::Array(Array<T> &source)
{
size=source.size;
content=new T[size];
for(int i=0;i<size;i++){content[i]=source.content[i];}
return *this;
}
template<class T>
Array<T>::~Array()
{
if(content!=NULL) delete []content;
}
template<class T>
Array<T>& Array<T>::operator =(Array<T> &source)
{
if(size<source.size)
{
delete []content;
size=source.size;
content=new T[size];
}
for(int i=0;i<size;i++){content[i]=source.content[i];}
return *this;
}
template<class T>
T& Array<T>::operator [](int index)
{
if (index<0||index>=size) ErrorExit(Out_Of_Bound);
return content[index];
}
template<class T>
Array<T>::operator T*()
{
return content;
}
template<class T>
int Array<T>::PeekSize()
{
return size;
}
template<class T>
void Array<T>::Resize(int n,Mode mode)
{
if(n<0) ErrorExit(Invalid_Size);
int t;
if(mode==Change)
t=n;
else
t=n+size;
T* temp=new T[t];
if (temp==NULL) ErrorExit(Allocation_Error);
int end=(t>size)?size:t;
for(int i=0;i<end;i++) temp[i]=content[i];
delete []content;
size=t;
content=temp;
}
#endif