/* -------------------------------------------------------------------- */
/* Program Name         : pointerDataClass.cpp                          */
/* Author               : Leen-Kiat Soh                                 */
/* Purpose              : This implements the pointerDataClass class.   */
/*                        To show how we do deep copy here.             */
/* Date                 : January 18, 2007                              */
/* Version              : 1.0                                           */
/* Notes                : Based on Malik (2003).                        */
/* -------------------------------------------------------------------- */

#include <iostream>
#include "pointerDataClass.h"

using namespace std;

/* -------------------------------------------------------------------- */
/* Name                 : constructor                                   */
/* Purpose              : To allocate an array of 10 integers to 'p'    */
/* Postconditions       : p is pointed to an array of 10 integers,      */
/*                        initialized from 1 to 10; and lenP is         */
/*                        initialized to 10.                            */
/* -------------------------------------------------------------------- */

pointerDataClass::pointerDataClass() {

   p = new int[10];
   for (int i = 0; i < 10; i++)  {
      p[i] = i*10;
      cout << p[i] << endl;
      }
   lenP = 10;

}; // end constructor
    
/* -------------------------------------------------------------------- */
/* Name                 : destructor                                    */
/* Purpose              : To deallocate an array of integers pointed to */
/*                        by 'p'                                        */
/* Postconditions       : p points to null.                             */
/* -------------------------------------------------------------------- */

pointerDataClass::~pointerDataClass()  {
   delete[] p;

}  // end destructor
    
/* -------------------------------------------------------------------- */
/* Name                 : copy constructor                              */
/* Purpose              : To (deep) copy the content of another object  */
/* Postconditions       : new array of elements is allocated and pointed*/
/*                        to by p.  Values of all data members are      */
/*                        changed to take the values of another object  */
/* -------------------------------------------------------------------- */

pointerDataClass::pointerDataClass(const pointerDataClass& otherObject)  {

   x = otherObject.x;
   lenP = otherObject.lenP;
   p = new int[lenP];   // allocate new memory spaces!!

   for (int i = 0; i < lenP; i++)
      p[i] = otherObject.p[i];   // copy the content one by one

}  // end copy constructor

/* -------------------------------------------------------------------- */
/* Name                 : overloading the assignment operator           */
/* Purpose              : To overload the assignment (=) operator       */
/* Postconditions       : new array of elements is allocated and pointed*/
/*                        to by p.  Values of all data members are      */
/*                        changed to take the values of another object  */
/* Notes                : very similar to the copy constructor          */
/* -------------------------------------------------------------------- */

const pointerDataClass& pointerDataClass::operator=
                                (const pointerDataClass & o1)  {
   if (this != &o1)  {

      x = o1.x;
      lenP = o1.lenP;
      p = new int[lenP];        // allocate new memory spaces!!

      for (int i = 0; i < lenP; i++)
         p[i] = o1.p[i];        // copy the content one by one

      }

   return *this;

}  // end overloaded assignment operator



