// demonstrates the use customization of assignment operator // mikhail nesterenko // 11/22/99 #include using namespace std; // just a class with two member function class myclass{ public: myclass(int); myclass(const myclass&); // copy constructor // assignment operator myclass& operator= (const myclass&); void printdata(); private: int *pint; }; // void constructor - allocates location for integer // variable myclass::myclass(int i){ pint=new int(i); } // copy constructor - copies the value held in the // memory location rather than the pointer myclass::myclass(const myclass &o){ pint=new int; *pint=*(o.pint); } // assignment operator // invoked by left operand object myclass& myclass::operator= (const myclass &o){ // avoid self-assignment if (this != &o) *pint=*(o.pint); //copy the value // return the object return *this; } // prints the value of the member variable void myclass::printdata(){ cout << "the value stored in the object is: "; cout << *pint; cout << endl; } // creates an object of myclass, prints // it's value int main(){ myclass ob1(1), ob2(2), ob3(3); ob2.printdata(); ob3=ob2=ob1; ob3=ob3; ob3.printdata(); }