// DYNARR.CPP // MICHAEL LEVY ,All Rights Reserved #include #include #include "dynarr.h" ///constructors template DynArr::DynArr() { arr=(T*)NULL; size=0; style='D'; } template DynArr::DynArr(int s) { style = 'F'; arr = new T[s]; if(arr==NULL) { cerr<<"Bad allocaion ,sorry not enough memory ?!\n"; exit(0); } size = s; } template DynArr::DynArr(const DynArr& rhs) { // Why to write the same code twice ? ,we use operator '=' // the only difference that we don't return a value here because , // it's a constructor operator=(rhs); } template DynArr& DynArr::operator=(const DynArr& rhs) { if(this==&rhs) return *this; if(arr!=NULL) delete []arr; arr=new T[rhs.size]; for(int i=0; i T& DynArr::operator[](int index) { if(index < size) return arr[index]; else if(style=='F') { cout<<"Overflow in Array\n"; exit(1); } else { T* new_ptr= new T[index+1]; int i; for( i=0; i void DynArr::Swap(T& a,T& b) { T c; c=a; a=b; b=c; } template void DynArr::SelectionSort(char order='a') { int small,i,j; if(order=='a') { for(i=0; i< size-1; i++) { small=i; for(j=i+1; j arr [small]) small=j; Swap(arr[i],arr[small]); } } }