c++day3
- 游戏开发
- 2025-08-17 13:27:02

#include <iostream> using namespace std; class Per { private: string name; int age; int *hight; int *weight; public: Per() { cout << "无参构造函数" << endl; } Per(string name, int age, int *hight, int *weight):name(name),age(age),hight(new int(*hight)),weight(new int(*weight)) { cout << "有参构造函数" << endl; } Per(const Per &other):name(other.name),age(other.age),hight(new int(*(other.hight))),weight(new int(*(other.weight))) { cout << "拷贝构造函数" << endl; } ~Per() { delete hight; delete weight; cout << "析构函数" << endl; } void show() { cout << "name: " << name << '\t'; cout << "age: " << age << '\t'; cout << "hight: " << *hight << '\t'; cout << "weight: " << *weight << '\t'; } }; class Stu { private: float score; Per p1; public: Stu() { cout << "无参构造函数" << endl; } Stu(float score, Per p1):score(score),p1(p1) { cout << "有参构造函数" << endl; } Stu(const Stu &other):score(other.score),p1(other.p1) { cout << "拷贝构造函数" << endl; } ~Stu() { cout << "析构函数" << endl; } void show() { p1.show(); cout << "score: " << score << endl; } }; int main() { int w = 70; int h = 178; Per p1("张三", 18, &h, &w); Stu s1(95, p1); s1.show(); Per p2(p1); Stu s2(s1); s2.show(); return 0; }