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