使用类的嵌套,并自定义析构函数
#include <iostream>
using namespace std;
class Per{
private:
string name;
int age;
double hight;
double weight;
public:
Per(string name,int age,double hight,double weight):name(name),age(age),hight(hight),weight(weight){
cout << "Per初始化完成" << endl;
}
~Per(){
cout <<"Per析构函数" << this << endl;
}
void dipaly();
};
class Stu{
private:
double score;
Per* p1;
public:
Stu(double score,Per p1):score(score),p1(new Per(p1)){
cout << "Stu初始化完成" << endl;
}
~Stu(){
cout << "Stu析构函数" << this << endl;
delete p1;
p1=nullptr;
}
void show();
};
void Stu::show(){
cout <<"score:" << score << endl;
cout << "&p1=" << p1 <<endl;
}
void Per::dipaly(){
cout << "name:" << name << " age:" << age << " hight:" << hight << " weight:" << weight << endl;
}
int main()
{
Per p("Tom",24,197.7,70.4);
Stu S(99.9,p);
cout << "&p=" << &p <<" &S=" << &S <<endl;
p.dipaly();
S.show();
return 0;
}