#include <iostream>
using namespace std;
class Person
{
string name;
int *age;
public:
Person(string name,int age):name(name),age(new int(age))
{}
~Person()
{
delete age;
}
Person(const Person &other):name(other.name),age(new int(*(other.age)))
{
cout << "P的拷贝构造" << endl;
}
Person &operator=(const Person &other)
{
this->name=other.name;
*(this->age)=*(other.age);
cout << "P的拷贝赋值" << endl;
return *this;
}
void show()
{
cout << *age << endl;
cout << name << endl;
}
};
class Stu:public Person
{
const double score;
public:
Stu(string name,int age,const double score):Person(name,age),score(score)
{
cout << "S的有参构造" << endl;
}
~Stu(){}
Stu(const Stu &other):Person(other),score(other.score)
{
cout << "S的拷贝构造" << endl;
}
Stu &operator=(const Stu &other)
{
cout << "S的拷贝赋值" << endl;
Person::operator=(other);
return *this;
}
void show()
{
cout << score << endl;
Person::show();
}
};
int main()
{
Stu s1("lisi",19,90);
s1.show();
Stu s2("wangwu",20,70);
s2=s1;
s2.show();
Stu s3=s1;
s3.show();
return 0;
}
C++ day5 作业
于 2024-04-28 19:22:39 首次发布
![](https://img-home.csdnimg.cn/images/20240711042549.png)