有以下类定义,按要求实现剩余功能
#include <iostream>
using namespace std;
class Person
{
private:
int age;
int *p;
public:
//无参构造
Person():p(new int(89))
{
age = 18;
cout << "无参构造" << endl;
}
//有参构造
Person(int age,int num)
{
this->age = age;
this->p=new int(num);
cout << "有参构造" << endl;
}
//拷贝构造函数
Person(Person &other)
{
age=other.age;
p=new int(*(other.p));//开辟新的空间,深拷贝
cout << "拷贝构造函数" << endl;
}
//拷贝赋值函数
Person &operator=(Person &other)
{
if(this!=&other)
{
age=other.age;
//构造函数已经开辟空间
*p=*(other.p);
cout << "拷贝赋值函数" << endl;
}
}
//析构函数
~Person()
{
delete p;
p=nullptr;
cout << "析构函数" << endl;
}
void show()
{
cout << p << endl;
}
};
int main()
{
int num=180;
Person str1(18,num);
Person str2=str1;//str2调用拷贝构造函数
//拷贝赋值函数
Person str3;
str3=str2;
return 0;
}