Some useful functions to work
with dynamic arrays are not realized in this program because it was
written only to demonstrate the principle of creation such arrays.
The definition of the class:
template<class T> class DynamicArray { long size; //the size of the array long count; //the count of elements in the array T* p; //the pionter to the beginning of the array
public: //constructors DynamicArray(long s = 10): size(s), count(0) { p = new T[size]; } DynamicArray(const DynamicArray& arr); //copying costructor
template<class T> void DynamicArray<T>::add(T x) { if(count >= size) { DynamicArray temp(*this); if(p) delete[] p; size += 10; p = new T[size]; for(int i = 0; i<temp.count; ++i) p[i] = temp.p[i]; } p[count++] = x; //add the element to the end of the array }
template<class T> void DynamicArray<T>::remove() { if(count) p[--count] = 0; //delete the last element (if the array isn't //empty) }