- 整理思维导图
- 复习课上代码
- 有以下类定义,写出该类的构造函数,析构函数,拷贝构造函数,要求,构造函数要创建出长度为10的字符串给name用,所有类对象的空间都是用new动态申请。
1.
#include <iostream>
using namespace std;
class Stu
{
string name;
int age;
int score;
int *high;
public:
//构造函数
Stu()
{
cout << "无参构造函数" <<endl;
}
Stu(string name,int age,int score,int *high)
{
this->name=name;
this->age=age;
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 <<endl;
cout << age << endl;
cout << score << endl;
cout << high <<endl;
cout << *high << endl;
}
};
int main()
{
string *name=new string[10]{"lisi"};
int *high=new int(190);
int *age=new int(20);
int *score=new int(90);
Stu s(*name,*age,*score,high);
s.show();
Stu p1;
Stu p2=s;
p2.show();
return 0;
}