// z6.7.2-assign.cc // #include using namespace std; class MyClass { public: MyClass(); ~MyClass(); MyClass& operator=(const MyClass& objToCopy); // Set member value dataObj void SetDataObj(const int setVal) { *dataObj = setVal; } // Return member value dataObj int GetDataObj() const { return *dataObj; } private: int* dataObj;// Data member }; // Default constructor MyClass::MyClass() { cout << "Constructor called." << endl; dataObj = new int; // Allocate mem for data *dataObj = 0; } // Destructor MyClass::~MyClass() { cout << "Destructor called." << endl; delete dataObj; } MyClass& MyClass::operator=(const MyClass& objToCopy) { cout << "Assignment op called." << endl; if (this != &objToCopy) { // 1. Don't self-assign delete dataObj; // 2. Delete old dataObj dataObj = new int; // 3. Allocate new dataObj *dataObj = *(objToCopy.dataObj); // 4. Copy dataObj } return *this; } int main() { MyClass classObj1; // Create object of type MyClass MyClass classObj2; // Create object of type MyClass // Set and print object 1 data member value classObj1.SetDataObj(9); // Copy class object using copy assignment operator classObj2 = classObj1; // Set object 1 data member value classObj1.SetDataObj(1); // Print data values for each object cout << "classObj1:" << classObj1.GetDataObj() << endl; cout << "classObj2:" << classObj2.GetDataObj() << endl; return 0; }