思维导图
有以下类定义,写出该类的构造函数,析构函数,拷贝构造函数,要求,构造函数要创建出长度为10的字符串给name用,所有类对象的空间都是用new动态申请。
#include <iostream>
using namespace std;
class Stu
{
string name;
int age;
int score;
int *high;
public:
//构造函数
Stu(string n,int a,int s,int *h):name(n),age(a),score(s),high(h)
{
cout << "构造函数" << endl;
}
//析构函数
~Stu()
{
delete high;
cout << "high空间释放成功" << endl;
cout << "析构函数" << endl;
}
//拷贝构造函数
Stu(const Stu &a):name(a.name),age(a.age),score(a.score)
{
this->high=new int(*(a.high));
cout << "拷贝构造函数" << endl;
}
void show()
{
cout << name << "\t" << age << "\t" << score << "\t" << &high << high << endl;
}
};
int main()
{
int h=180;
Stu *p=new Stu{"zhangsan",18,99,&h};
p->show();
Stu *p1=new Stu(*p);
p1->show();
delete p;
delete p1;
return 0;
}