cpp
#include <iostream>
using namespace std;
//Per 类
class Per
{
string name ;
double *high;
double *weight;
public:
//有参构造函数 初始化列表方法
Per (string name, double h,double w):name(name),high(new double(h)),weight(new double(w))
{
cout << "有参构造函数" << endl;
}
//析构函数 释放指针
~Per()
{
delete high;
delete weight;
high =nullptr;
weight =nullptr;
cout << "析构函数" << endl;
}
//由外界调用输出结果
void show()
{
cout << "姓名:" << name << "身高:" << *high << "体重:" << *weight << endl;
}
};
//Stu 类
class Stu
{
double score;
Per p1;
public:
// 有参构造函数 初始化列表法 并初始化p1
Stu(string n,double h,double w,double score):p1(n,h,w),score(score)
{
cout << "有参构造函数" << endl;
}
// 析构函数
~Stu()
{
cout << "析构函数" << endl;
}
//外界调用输出
void show ()
{
p1.show();
cout << "成绩:" << score << endl;
}
};
int main()
{
Stu s1("zhangsan",178,70,89);
s1.show();
return 0;
}