有以下类定义,写出该类的构造函数,析构函数,拷贝构造函数,要求,构造函数要创建出长度为10的字符串给name用,所有类对象的空间都是用new动态申请。
#include <iostream>
using namespace std;
class Stu
{
string name;
int age;
int score;
int *high;
public:
//构造函数
Stu()
{
cout<<"无参构造"<<endl;
}
Stu(int age,string name,int score,int *high)
{
this->age=age;
this->name=name;
this->score=score;
this->high=high;
cout<<"有参构造函数"<<endl;
}
//析构函数
~Stu()
{
cout<<"析构函数"<<endl;
}
//拷贝构造函数
Stu(Stu &p):name(p.name),age(p.age),score(p.score)
{
this->high=new int;
*(this->high)=*(p.high);
}
void show()
{
cout << name <<age<<score<<endl;
cout<<high<<*high<<endl;
}
};
int main()
{
string *name=new string[10]{"yzz"};
int *high=new int(181);
int *age=new int(24);
int *score=new int(89);
Stu s(*age,*name,*score,high);
s.show();
Stu pre;
Stu per1=s;
per1.show();
}