有以下类定义,写出该类的构造函数,析构函数,拷贝构造函数,要求,所有类对象的空间都是用new动态申请。
class stu
{
string name;
int age;
int score;
int *high;
};
#include <iostream>
using namespace std;
class stu{
string name; //0-31
int age; //32-35
int score; //36-40
int *high; //41-47
//48
public:
stu(string name,int age,int score,int high) :name(name),age(age),score(score),high(new int (high))
{
//赋值
// this->name=name;
// this->age=age;
// this->score=score;
// this->high=high;
}
stu (const stu &STU):name(STU.name),age(STU.age),score(STU.score),high(STU.high)
{
//this->high=new int (*STU.high);
memcpy(this->high,STU.high,sizeof(*(STU.high)));
cout<<"深拷贝"<<endl;
}
void show()
{
cout<<"姓名:"<<name<<" "<<"年龄:"<<age<<" "<<"成绩:"<<score<<" "<<"身高:"<<*high<<endl;
}
//析构函数
~stu()
{
delete high;
//cout<<"stu的析构函数"<<endl;
}
};
int main()
{
//定义类指针,在new申请空间时才调用构造函数
stu *SS=new stu("李四",20,99,188);
SS->show();
cout<<"深拷贝结果"<<endl;
//拷贝
stu *STD=SS;
STD->show();
//释放堆区空间
delete SS;
SS=nullptr;
delete STD;
STD=nullptr;
return 0;
}