title: operator overloading for Array class
body: <p>i am trying to overload operators &lt;&lt; >> != == = and [] for  Array class.
  The app crashes on run, though no compilation errors are shown.
  what could possibly be wrong? IDE used dev c++</p>
  
  <p>Here's array.h</p>
  
  <pre><code>#ifndef ARRAY_H
  #define ARRAY_H
  
  #include &lt;iostream&gt;
  using namespace std;
  
  class Array{
    friend ostream &amp; operator &lt;&lt; ( ostream &amp;, const Array &amp; );
    friend istream &amp; operator &gt;&gt; ( istream &amp;, Array &amp;);
    private:
           int size;
           int * ptr;
    public:
           Array ( int = 10 );
           Array ( const Array &amp; ); //copy constructor
           ~Array ();
           const Array &amp;operator=( const Array &amp; ); 
           bool operator == ( const Array &amp; ) const; 
           bool operator != ( const Array &amp; ) const;
           const int operator [] (int) const; 
           int getSize() const;            
  };
  
  #endif
  </code></pre>
  
  <p>and now array.cpp</p>
  
  <pre><code>#include &lt;iostream&gt;
  using namespace std;
  #include "array.h"
  
  Array::Array (int sze ){ //default constructor edited
           size = (sze &gt; 0 ? sze : 10);
           ptr = new int [ size ];
           for (int i = 0;  i &lt; size; i++)
               ptr[ i ] = 0; //initial values
  }
  Array::Array (const Array &amp; arr ): size(arr.size){
           ptr = new int [size];
           for ( int i = 0; i&lt; size; i++)
               ptr [ i ] = arr.ptr [ i ];
  }
  Array::~Array(){
           delete [] ptr;
  }
  const Array &amp;Array :: operator= ( const Array &amp; right){//IMPO
           if(&amp;right != this){ //edited self assignment test
                     if(size != right.size){//diff sized arrays
                             delete [] ptr; //reclaim space
                             size = right.size; 
                             ptr = new int [ size ]; //space created
                     }
           }
           for(int i=0; i&lt;size; i++)
                   ptr[ i ] = right.ptr[ i ];
           return *this;     //enables cascading a=b=c       
  }
  bool Array::operator == ( const Array &amp; right) const{
           if ( size != right.size )
              return false;
           for ( int i =0; i &lt; size; i++ ){
               if ( ptr [ i ] != right.ptr[ i ] )
                  return false;
           }
           return true;
   }
  bool Array::operator != ( const Array &amp; right ) const{ //edited
           return ! (*this == right);
  }
  const int Array::operator [] (int subscript) const{
           if(subscript &gt;=0 &amp;&amp; subscript &lt; size)
              return ptr[ subscript ];      
  }
  int Array::getSize() const{ return size; }  
  //friend functions not in .h
  ostream &amp; operator &lt;&lt; ( ostream &amp; output, const Array &amp; array){
           for (int i = 0; i &lt; array.size; i++)
               output &lt;&lt; array.ptr[i] ; 
  }
  istream &amp; operator &gt;&gt; ( istream &amp; input, Array &amp; array){
           for (int i = 0; i &lt; array.size; i++)
               input &gt;&gt; array.ptr[i];
  }
  </code></pre>
  
  <p>now main.cpp</p>
  
  <pre><code>#include &lt;cstdlib&gt;
  #include &lt;iostream&gt;
  #include "array.h" // " " not &lt;&gt;
  using namespace std;
  
  int main(int argc, char *argv[])
  {
  Array a1(7),a2 (-1),a4; //changed a2
  cout&lt;&lt;"Input "&lt;&lt;a1.getSize()&lt;&lt;" integers for Array object a1 and "&lt;&lt;a2.getSize()&lt;&lt;" integers for Array objecta2\n";
  cin&gt;&gt;a1&gt;&gt;a2;
  cout&lt;&lt;"a1 and a2 are\n";
  cout&lt;&lt;a1&lt;&lt;endl&lt;&lt;a2;
  cout&lt;&lt;"a1!=a2 : "&lt;&lt;(a1!=a2)&lt;&lt;endl;
  cout&lt;&lt;"a1 ==a2: "&lt;&lt;(a1==a2)&lt;&lt;endl;
  cout&lt;&lt;"Printing a1[5] : "&lt;&lt;a1[5]&lt;&lt;endl;
  Array a3(a1); 
  a4 = a3;
  
  system("PAUSE");
  return EXIT_SUCCESS;
  }
  </code></pre>