https://www.zhixi.com/view/9e899ee0
- #include
-
- using namespace std;
-
- class Per
- {
- private:
- string name;
- int age;
- int *h;
- int *w;
- public:
- //构造函数
- Per(string name,int age,int h,int w):name(name),age(age),h(new int (h)),w(new int (w))
- {
- cout << "Per:构造函数->" << this << endl;
- }
- //析构函数
- ~Per()
- {
- cout << "Per:析构函数->" << this << endl;
- }
- //拷贝构造函数
- Per(const Per &other):name(other.name),age(other.age),h(other.h),w(other.w)
- {
- cout << "Per:拷贝构造函数" << this << endl;
- }
- };
-
- class Stu
- {
- private:
- int grade;
- Per p1;
- public:
- //构造函数
- Stu(int grade,Per p1):grade(grade),p1(p1)
- {
- cout << "Stu:构造函数->" << this << endl;
- }
- //析构函数
- ~Stu()
- {
- cout << "Stu:析构函数->" << this << endl;
- }
- //拷贝构造函数
- Stu(const Stu &other):grade(other.grade),p1(other.p1)
- {
- cout << "Stu:拷贝构造函数->" << this << endl;
- }
- };
-
- int main()
- {
- Per p1("zhangsan",20,170,120);
- Stu s1(90,p1);
- return 0;
- }