// This example overloads the = operator // It also demonstrates the use of "this" #include using namespace std; class Person { private: int *itsAge; int *itsWeight; public: Person(); // default constructor int GetAge() const { return *itsAge; } int GetWeight() const { return *itsWeight; } void SetAge(int age) { *itsAge = age; } Person operator=(const Person &); // overloads = operator }; Person::Person() // implement the default constructor { itsAge = new int; itsWeight = new int; *itsAge = 5; *itsWeight = 9; } Person Person::operator=(const Person &obj) { if (this == &obj) // if the objects are the same objects return *this; delete itsAge; delete itsWeight; itsAge = new int; itsWeight = new int; *itsAge = obj.GetAge(); *itsWeight = obj.GetWeight(); return *this; } int main() { Person myPerson; cout << "myPerson's age: " << myPerson.GetAge() << endl; cout << "Setting myPerson to 6...\n"; myPerson.SetAge(6); Person anotherPerson; cout << "AnotherPerson's age = " << anotherPerson.GetAge() << endl; cout << "copying myPerson to anotherPerson...\n"; anotherPerson = myPerson; cout << "anotherPerson's age = " << anotherPerson.GetAge() << endl; getchar(); return 0; }