Implementation
Implementation
#include <iostream>
using namespace std;
class MyClass {
public:
MyClass();
~MyClass();
MyClass& operator=(const MyClass& objToCopy);
void SetDataObj(const int setVal) {
*dataObj = setVal;
}
int GetDataObj() const {
return *dataObj;
}
private:
int* dataObj;
};
MyClass::MyClass() {
cout << "Constructor called." << endl;
dataObj = new int;
*dataObj = 0;
return;
}
MyClass::~MyClass() {
cout << "Destructor called." << endl;
delete dataObj;
return;
}
MyClass& MyClass::operator=(const MyClass& objToCopy) {
cout << "Assignment op called." << endl;
if (this != &objToCopy) {
delete dataObj;
dataObj = new int;
*dataObj = *(objToCopy.dataObj);
}
return *this;
}
int main() {
MyClass tempClassObj1;
MyClass tempClassObj2;
tempClassObj1.SetDataObj(9);
tempClassObj2 = tempClassObj1;
tempClassObj1.SetDataObj(1);
cout << "obj1:" << tempClassObj1.GetDataObj() << endl;
cout << "obj2:" << tempClassObj2.GetDataObj() << endl;
return 0;
}