title: What is the difference between a destructor and a funtion that deallocates?
body: <p>What is the difference between the destructor and the DeAllocate function in this code?</p>
  
  <p>I don't get it they look like the same thing to me.They do the exact same thing, why would you ever need a function like DeAllocate? Destructor has the benefit of being called automatically, but DeAllocate doesn't.</p>
  
  <pre><code>#include &lt;iostream&gt;
  using namespace std;
  
  const int SIZE=5;
  
  class ARRAY_CLASS
  {
  public:
      ARRAY_CLASS();//default constructor
      ~ARRAY_CLASS(); //destructor
      void Add(int); //mutator
      void Print(); //accessor
      int * Get_Address(); //accessor
      void DeAllocate(); //mutator
  private:
  
      int *A;
      int count;
  };
  
  ARRAY_CLASS::ARRAY_CLASS()
  {
      cout&lt;&lt;"Default constructor has been called\n";
      A = new int[SIZE];
      count = 0;
  }
  
  
  ARRAY_CLASS::~ARRAY_CLASS()
  {
      cout&lt;&lt;"The Destructor has been Called!\n";
      delete [ ] A;
      A=0;
      count = 0;
  }
  
  void ARRAY_CLASS::Add(int item)
  {
      if (count&lt;SIZE)
          A[count++]=item;
      else
          cout&lt;&lt;"Array Full\n";
  }
  void ARRAY_CLASS::Print()
  {
      for(int i=0; i&lt;count; i++)
          cout&lt;&lt;"A[i] = "&lt;&lt;A[i]&lt;&lt;endl;
  }
  
  int * ARRAY_CLASS::Get_Address()
  {
      return A;
  }
  
  void ARRAY_CLASS::DeAllocate()
  {
      delete [ ] A;
      A = 0;
      count = 0;
  }
  
  int main()
  {
  
      ARRAY_CLASS B;
  
      B.Add(1);
      B.Add(2);
      B.Add(3);
      B.Add(4);
      B.Add(5);
  
      B.Print();
  
      ARRAY_CLASS A = B;
  
      cout&lt;&lt;"A holds address location = "&lt;&lt;A.Get_Address()
      &lt;&lt;" and B holds address location "&lt;&lt;B.Get_Address()&lt;&lt;endl;
  
      B.DeAllocate();
      A.Print();
  
      return 0;
  }
  </code></pre>