#include <iostream>
using namespace std;
//定义一个类(人)
class Per
{
private:
string name;//姓名
int age;//年龄
//体重和身高另存堆空间
double* height;//身高
double* weight;//体重
public:
//定义构造函数,并且初始化
//运用初始化列表
Per(string name,int age,double height,double weight):name(name),age(age),height(new double(height)),weight(new double(weight))
{
//提示信息,表示调用该函数
cout << "Structure" << endl;
}
//定义析构函数
~Per()
{
//释放堆空间
delete height;
delete weight;
//提示信息,表示调用该函数
cout << "Destruction" << endl;
}
//定义拷贝构造函数
//运用初始化列表
Per(const Per &other):name(other.name),age(other.age),height(new double(*other.height)),weight(new double(*other.weight))
{
//提示信息,表示调用该函数
cout << "Per::copy" << endl;
}
//定义输出函数
void show()
{
cout << "name:" << name << endl;
cout << "age:" << age << endl;
cout << "height:" << *height << endl;
cout << "weight:" << *weight << endl;
}
};
//定义学员类,包含分数和个人信息的类
class Stu
{
private:
double score;//分数
Per p1;//个人信息的类
public:
//定义构造函数并初始化
//运用初始化列表
Stu(double score,Per p1):score(score),p1(p1)
{
//提示信息,表示调用该函数
cout << "Structure" << endl;
}
//定义析构函数
~Stu()
{
//提示信息,表示调用该函数
cout << "Destruction" << endl;
}
//定义拷贝构造函数
//运用初始化列表
Stu(const Stu &other):score(other.score),p1(other.p1)
{
//提示信息,表示调用该函数
cout << "Stu::copy" << endl;
}
//定义输出函数
void show()
{
cout << "score:" << score << endl;
p1.show();//对个人信息的输出函数调用
}
};
int main()
{
Per per("XX",25,190,78.5);
per.show();
Stu stu(1024,per);
stu.show();
Per per_1(per);
per_1.show();
Stu stu_1(stu);
stu_1.show();
return 0;
}
2023.10.09
最新推荐文章于 2024-07-08 21:32:26 发布