day44 1228
2023-12-28 20:32:33
作业1:
#include <iostream>
using namespace std;
class Person
{
int *age;
string &name;
public:
Person(int age,string &name):age(new int(age)),name(name)
{
cout << "Person的构造函数" <<endl;
}
~Person()
{
delete age;
cout << "Person的析构函数" << endl;
}
Person(Person &other):age(new int(*(other.age))),name(other.name)
{
cout << "Person的拷贝构造函数" << endl;
}
Person &operator = (Person &other)
{
*(this->age) = *(other.age);
this->name = other.name;
cout << "Person的拷贝赋值函数" << endl;
return *this;
}
void show();
};
class Stu
{
double *score;
Person p1;
public:
Stu(double score,Person p):score(new double(score)),p1(p)
{
cout << "Stu的构造函数" << endl;
}
~Stu()
{
delete score;
cout << "Stu的析构函数" << endl;
}
Stu(Stu &other):score(new double(*(other.score))),p1(other.p1)
{
cout << "Stu的拷贝构造函数" <<endl;
}
Stu &operator = (Stu &other)
{
*(this->score) = *(other.score);
this->p1 = other.p1;
cout << "Stu的拷贝赋值函数" << endl;
return *this;
}
void show(string);
};
void Person::show()
{
cout << "age = " << age << endl;
cout << "*age = " << *age << endl;
cout << "name = " << name << endl;
cout << "&name = " << &name << endl;
}
void Stu::show(string remind)
{
cout << "-----------" << remind <<"------------" << endl;
cout << "score = " << score <<endl;
cout << "*score = " << *score << endl;
cout << "&p1 = " << &p1<<endl;
p1.show();
cout << "-------------------------" << endl;
}
int main()
{
string name1 = "张三";
Person p1(18,name1);
Stu s1(99.9,p1);
s1.show("s1");
Stu s2(s1);
s2.show("s2");
string name2 = "李四";
Person p2(19,name2);
Stu s3(88.8,p2);
s2 = s1;
s2.show("s2");
s3.show("s3");
return 0;
}
现象:
文章来源:https://blog.csdn.net/weixin_51788918/article/details/135276590
本文来自互联网用户投稿,该文观点仅代表作者本人,不代表本站立场。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。 如若内容造成侵权/违法违规/事实不符,请联系我的编程经验分享网邮箱:veading@qq.com进行投诉反馈,一经查实,立即删除!
本文来自互联网用户投稿,该文观点仅代表作者本人,不代表本站立场。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。 如若内容造成侵权/违法违规/事实不符,请联系我的编程经验分享网邮箱:veading@qq.com进行投诉反馈,一经查实,立即删除!