
- #include
-
- using namespace std;
-
- class Per
- {
- private:
- string name;
- int age;
- float* height;
- float* weight;
- public:
- Per(){cout << "Per::无参构造函数" << endl;}
- Per(string name,int age,float* height,float* weight):name(name),age(age),height(new float(*height)),weight(new float(*weight))
- {
- cout << "Per::有参构造函数" << endl;
- }
- ~Per()
- {
- cout << "Per::析构函数" << endl;
- delete height;
- delete weight;
- }
- Per(const Per &other):name(other.name),age(other.age),height(new float(*(other.height))),weight(new float(*(other.weight)))
- {
- cout << "构造拷贝函数" << endl;
- }
- };
-
- class Stu
- {
- private:
- double score;
- Per p1;
- public:
- Stu(){cout << "Stu::无参构造函数" << endl;}
- Stu(double score,Per p1):score(score),p1(p1)
- {
- cout << "Sti::有参构造函数" << endl;
- }
- ~Stu()
- {
- cout << "Stu::析构函数" << endl;
- }
- Stu(const Stu &other):score(other.score),p1(other.p1)
- {
- cout << "拷贝构造函数" << endl;
- }
- };
-
-
- int main()
- {
- float h = 170;
- float w = 87;
- Per p1;
- Per p2("zhangsan",21,&h,&w);
- Stu s1(100,p2);
- Per p3(p2);
- Stu s2(s1);
-
- return 0;
- }
