Define your own generic array : generic array « template « C++ Tutorial






#include<iostream.h>
#include<iomanip.h>
template<class T>
class Array
{
  T *elems;
  int size;
  public:
    Array(int s)
    {
      size=s;
      elems=new T[size];
      for(int i=0;i<size;i++)
         elems[i]=0;
    }

    ~Array()
    {
      delete elems;
    }

    T& operator[](int index)
    {
      return elems[index];
    }

    void operator=(T temp)
    {
       for(int i=0;i<size;i++)
               elems[i]=temp;
    }
};

int main()
{
  int n=10;
  Array<int>arr1(n);
  Array<char>arr2(n);

  for(int i=0;i<n;i++)
  {
    arr1[i]='a'+i;
       arr2[i]='a'+i;
  }
  cout <<endl;
  for(int i=0;i<n;i++){
         cout<<setw(8)<<arr1[i]<<setw(8)<<arr2[i]<<endl;
  }
}
97       a
      98       b
      99       c
     100       d
     101       e
     102       f
     103       g
     104       h
     105       i
     106       j








13.11.generic array
13.11.1.Define your own generic array
13.11.2.A generic safe array example