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://1.无参构造Person():p(new int(89)){age = 18;}//2.有参构造Person(int age,int num){this->age = age;this->p=new int(num);}//3.拷贝构造函数Person(Person &other){age=other.age;//浅拷贝
// p=other.p;//深拷贝p=new int;*p=*(other.p);cout << "Person拷贝构造函数" << endl;}//4.拷贝赋值函数Person &operator=(Person &other){if(this!=&other){age = other.age;//浅拷贝//point = other.point;//深拷贝*p = *(other.p);cout << "Person拷贝赋值函数" << endl;}return *this; //返回类对象本身}//5.析构函数~Person(){delete p;p=nullptr;cout << "释放那个了p指向的空间" << endl;cout << "Person的析构函数" << endl;}void show(){cout<<"age="<<age<<endl;cout<<"*p="<<*p<<endl;cout<<"p="<<p<<endl;}
};int main()
{Person k1;cout<<"k1:"<<endl;k1.show();cout<<"-----------------------------"<<endl;Person k2(20,99);cout<<"k2:"<<endl;k2.show();cout<<"-----------------------------"<<endl;cout<<"---------拷贝构造函数----------"<<endl;Person k3=k2;cout<<"k3:"<<endl;k3.show();cout<<"-----------------------------"<<endl;cout<<"---------拷贝赋值函数----------"<<endl;Person k4;k4=k2;cout<<"k4:"<<endl;k4.show();cout<<"-----------------------------"<<endl<<endl;return 0;
}