2023.10.09

#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;
}

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值