设计一个Per类,类中包含私有成员:姓名、年龄、指针成员身高、体重,再设计一个Stu类,类中包含私有成员:成绩、Per类对象p1,设计这两个类的构造函数、析构函数。
#include <iostream>
using namespace std;
class Stu
{
private:
double sroce;
public:
Stu(double sroce):sroce(sroce)
{
cout << "Stu::有参构造函数" << endl;
}
~Stu()
{
cout << "Stu::析构函数" << endl;
}
void show()
{
cout << sroce << endl;
}
};
class Per
{
private:
string name;
int age;
double *height;
double *weight;
Stu s;
public:
//构造函数初始化
Per(string name,int age,double height,double weight,double sroce):name(name),age(age),height(new double(height)),weight(new double(weight)),s(sroce)
{
cout << "Per::有参构造函数" << endl;
}
//析构函数释放堆区空间
~Per()
{
cout << "Per::析构函数" << endl;
delete height;
height=nullptr;
delete weight;
weight=nullptr;
}
//输出初始化之后的值
void show()
{
cout << name << " " << age << " " << *height << " " << *weight << " ";
s.show();
}
};
int main()
{
Per p1("lisi",18,178.9,140.5,99.9);
p1.show();
return 0;
}
思维导图: