#include <iostream>
class MyClass {
public:
int data;
// Assignment operator overload
MyClass& operator=(const MyClass& other) {
// Check for self-assignment
if (this != &other) {
// Copy the data from the right-hand side object to the left-hand side object
this->data = other.data;
}
// Return a reference to the left-hand side object
return *this;
}
};
int main() {
// Create two objects of MyClass
MyClass obj1, obj2;
// Assign a value to the data member of obj1
obj1.data = 42;
// Use the assignment operator to copy the data from obj1 to obj2
obj2 = obj1;
// Display the data member of obj2
std::cout << "obj2.data: " << obj2.data << std::endl;
return 0;
}