// Temparr.C -example of array with templates #include template class ArrayList{ private: Type *arr; int arrSize; public: ArrayList(int sizeIn = 5); ArrayList (const ArrayList &other); ~ArrayList(); ArrayList &operator=(const ArrayList &other); Type &operator[](int index); const Type &operator[](int index) const; int getSize() const; private: void copy(const ArrayList &other); void init(); void freeMem(); }; // default constructor template ArrayList::ArrayList(int sizeIn):arrSize(sizeIn){ if (arrSize < 0) arrSize = 5; arr = new Type[arrSize]; } // copy constructor template ArrayList::ArrayList(const ArrayList &other){ copy(other); } // overloaded operator= template ArrayList & ArrayList::operator=(const ArrayList &other){ if(this != &other){ freeMem(); copy(other); } return *this; } // destructor template ArrayList::~ArrayList(){ freeMem(); } // copy helper template void ArrayList::copy(const ArrayList &other){ init(); arrSize = other.arrSize; arr = new Type[arrSize]; } // init helper template void ArrayList::init(){ arr = NULL; arrSize = 0; } // freeMem helper template void ArrayList::freeMem(){ delete [] arr; arr = NULL; // to be safe } // operator[] non-const version template Type & ArrayList::operator[](int index){ return arr[index]; } // operator= const version template const Type & ArrayList::operator[](int index) const{ return arr[index]; } // size template int ArrayList::getSize() const{ return arrSize; } main(){ ArrayList list(10); for(int i = 0; i < 10 ; i++){ list[i] = 2*i; cout << list[i] << " "; } cout << endl; return 0; }