User Tools

Site Tools


c_-_c_templates:class_template_-_array

C - C++ Templates - Class Template - Array

#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;
  }
}
c_-_c_templates/class_template_-_array.txt · Last modified: 2020/07/15 09:30 by 127.0.0.1

Donate Powered by PHP Valid HTML5 Valid CSS Driven by DokuWiki