作业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;
}
现象:
思维导图