1.整理思维导图
2.有以下类定义,按要求实现剩余功能
#include <iostream>
using namespace std;
class Person {
private:
int age;
int *p;
public:
// 无参构造
Person() : p(new int(89)) {
age = 18;
cout << "无参构造" << endl;
show();
}
// 有参构造
Person(int age, int num) {
this->age = age;
this->p = new int(num);
cout << "有参构造" << endl;
show();
}
// 拷贝构造函数
Person(const Person &other) {
age = other.age;
p = new int(*(other.p));
cout << "调用拷贝构造函数" << endl;
show();
}
// 拷贝赋值函数
Person &operator=(const Person &other) {
if (this != &other) {
age = other.age;
delete p;
p = new int(*(other.p));
cout << "调用拷贝赋值函数" << endl;
show();
}
return *this;
}
// 析构函数
~Person() {
delete p;
p = nullptr;
cout << "调用析构函数" << endl;
}
void show() {
cout << "address: " << p << " " << "age: " << age << " " << "num: " << *p << endl;
}
};
int main() {
Person p1; // 无参构造
Person p2 = p1; // 拷贝构造
Person p3(25, 10000); // 有参构造
Person p4; // 无参构造
p4 = p3; // 拷贝赋值
return 0;
}