- 整理思维导图
- 在课上练习的基础上,完成拷贝赋值函数
- 完成下列类
#include <iostream>
using namespace std;
class person
{
string name;
int age;
char sex;
public:
int *p;
person()
{
cout<<"person无参构造"<<endl;
};
person(const string n,int a,char s):name(n),age(a),sex(s)
{
cout<<"person有参构造"<<endl;
}
person(const person &other):p(new int(*(other.p)))
{
this->name=other.name;
this->age=other.age;
this->sex=other.sex;
}
person &operator=(const person &other)
{
if(&other!=this)
{
this->name=other.name;
this->age=other.age;
this->sex=other.sex;
}
return *this;
}
void show()
{
cout<<name<<age<<sex<<endl;
}
~person()
{
delete p;
}
};
class stu
{
person p;
double *score;
public:
stu():p("张三",18,'m'),score(new double)
{
cout<<"stu无参构造"<<endl;
}
stu(const string n,int a,char s,double score):p(n,a,s),score(new double(score))
{
cout<<"stu有参构造"<<endl;
}
void show()
{
p.show();
cout<<*score<<endl;
}
~stu()
{
delete score;
}
};
int main()
{
person p1("zhangsan",18,'m');
person p2(p1);
person p3;
p3=p2;
p1.show();
p2.show();
p3.show();
cout<<(p1.p)<<' '<<(p2.p)<<' '<<(p3.p)<<endl;
return 0;
}