title: ArrayList implementation C++
body: <p>I need to write an IntArray implementation for college. I'm wondering if my code could be better. Is it efficient. In the header file are the methods listed that we need to write. thanks</p>
  
  <p>Header file : </p>
  
  <pre><code> #ifndef INTARRAY_H_
   #define INTARRAY_H_
  
   #include &lt;iostream&gt;
   using namespace std;
  
   class IntArray{
   private:
       int length;
       int * data;
  public:
      IntArray(int size = 0);
      IntArray(const IntArray&amp; other);
      IntArray&amp; operator=(const IntArray&amp; original);
  
      int getSize() const { return length; };
      int&amp; operator[](unsigned int i);
      void resize(unsigned int size);
      void insertBefore(int value, int index);
  
      friend ostream&amp; operator&lt;&lt;(ostream&amp; out, const IntArray&amp; list);
  
      ~IntArray(){ delete[] data; };
  
   };
   #endif
  </code></pre>
  
  <p>Cpp file:</p>
  
  <pre><code> #include "IntArray.h"
  
   IntArray::IntArray(int size){
       length = size;
       data = new int[size];
       for(int i=0; i&lt;size; i++){
           data[i] = 0;
       }
   }
  
   IntArray::IntArray(const IntArray&amp; other){
       length = other.length;
       data = new int[length];
       for(int i=0; i&lt;length; i++){
           data[i] = other.data[i];
       }
   }
  
  IntArray&amp; IntArray::operator =(const IntArray&amp; other){
  if(this = &amp;other){
      return *this;
  }
  length = other.length;
  data = new int[length];
  for(int i = 0; i &lt;length;i++){
      data[i] = other[i];
  }
  return *this;
  }
  
   int&amp; IntArray::operator[](unsigned int i){
        if(i&gt;=this-&gt;length) throw "Index out of bounds";
        return data[i];
   }
  
   ostream&amp; operator&lt;&lt;(ostream&amp; out, const IntArray&amp; list){
        for(int i=0; i&lt;list.getSize(); i++){
           out &lt;&lt; list.data[i] &lt;&lt; " ";
        }
       return out;
   }
  
   void IntArray::resize(unsigned size){
       if (size &lt;= length){
          length = size;
          return;
       }
       int * temparr = new int[size];
       // copy data
       for (unsigned int i = 0; i &lt; length; ++i){
          temparr[i] = data[i];
       }
       delete [] data; 
       data = temparr; 
       length = size; 
   }
  
   void IntArray::insertBefore(int value, int index){
       resize(length+1);
       for(int i=length-1; i&gt;index; i--){
            data[i] = data[i-1];
       }
       data[index] = value;
    }
  </code></pre>