IntArray implementation I need to write an IntArray implementation for college. I'm wondering if my code could be better and if it is efficient. In the header file are the methods listed that we need to write.

Header file:

 #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


Cpp file:

    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;
}
void IntArray::resize(unsigned size){
      if (size &lt;= length){
        length = size;
        return;
     }
     int * temparr = new int[size];
     // copy data
     for (int i = 0; i &lt; length; ++i){
        temparr[i] = data[i];
     }
     delete [] data;
     data = temparr;
     length = size;
}

int&amp; IntArray::operator [](unsigned i){
    if(i &gt;= length || i &lt; 0){
        throw "index out of bounds";
    }
    return data[i];
 }

 void IntArray::insertBefore(int d, int index){
     if(index &gt;= length){
         throw "index out of bounds";
     }
     resize(length + 1);
     for(int i = length -1;i&gt;index;i--){
          data[i] = data[i-1];
     }
     data[index] = d;
 }
Main changes:


Assuming you keep an int length, check for size &gt;= 0 in constructor IntArray(int size = 0);, and in void IntArray::insertBefore(int d, int index)
You thought about delete[] data in the destructor. However, there are other places in your code where data needs to be deleted: each time you change data's value, you need to deallocate what used to be pointed by data. Ie, delete[] data before any data =


You added data = new int[length]; in the copy constructor, so you've solved the out of bound issue, but introduced a memory leak instead. You need to deallocate data first.
Same for the assignment constructor.
resize had an out-of-bound issue you've fixed 



Code used to be: 

void IntArray::resize(unsigned int size){
    for(int i=length; i&lt;size; i++){
        data[i] = 0;
    }
    length = size;
}


(Note: you're not supposed to edit your code, or put your updated code in a separate section, at the bottom of your question)



Opinion-based


Maybe use an unsigned type (like std::size_t) instead of the signed int for the length.


Pros:


You enforce the invariant "the length can only be positive"


Cons:


Special values (like -1 for "Index not found") will look like regular values, higher risk of underflow.
(see Herb Sutter comment at 44:30)




Other


Superfluous this-&gt; in operator[](int index)
Style: pick one of length or getSize()to get the data length, but don't mix the two (Here length is fine, in classes where you'd like to make a check before accessing the value, getSomething() is better.
Add a const version of operator[](int index)
Don't add an extra space at the end of operator&lt;&lt; (which you removed by mistake)




Going further

It depends on the assignment.


Optimization: Introducing capacity to reduce the number of allocations/deallocations
Add iterators
Add convenience methods (bool empty() const;, int&amp; front();)
Add typedefs like typedef int value_type (useful for for metaprogramming)
Add relationship operators (at least == and !=)




@glampert made excellent points I forgot, check his answer.