华清远见嵌入式学习——C++——作业3

作业要求:

代码:

#include <iostream>

using namespace std;

class Per
{
private:
    string name;
    int age;
    double *high;
    double *weight;
public:
    //有参构造函数
    Per(string n,int a,double h,double w):name(n),age(a),high(new double(h)),weight(new double(w))
    {
        cout << "Per实例化对象构造成功" << endl;
        cout << name << " " << age << " " << *high << " " << *weight << endl;
    }
    //析构函数
    ~Per()
    {
        delete(high);
        delete(weight);
        high = nullptr;
        weight = nullptr;
        cout << "Per对象析构成功" << endl;
    }
    //拷贝构造函数(深拷贝)
    Per(const Per &o):name(o.name),age(o.age),high(new double(*(o.high))),weight(new double(*(o.weight)))
    {
        cout << "Per对象拷贝构造成功" << endl;
        cout << name << " " << age << " " << *high << " " << *weight << endl;
    }
    //显示函数
    void show()
    {
        cout << name << " " << age << " " << *high << " " << *weight << " ";
    }

};

class Stu
{
private:
    double score;
    Per p1;
public:
    //有参构造函数
    Stu(double s,string n,int a,double h,double w):score(s),p1(n,a,h,w)
    {
        cout << "Stu实例化对象构造成功" << endl;
        p1.show();
        cout << score << endl;
    }
    //析构函数
    ~Stu()
    {
        cout << "Stu对象析构成功" << endl;
    }
    //拷贝构造函数(浅拷贝)
    Stu(const Stu &o):score(o.score),p1(o.p1)
    {
        cout << "Stu对象拷贝构造成功" << endl;
        p1.show();
        cout << score << endl;
    }
};

int main()
{
    cout << "请输入姓名、年龄、身高、体重、成绩:" << endl;
    string name;
    int age;
    double high,weight,score;

    cin >> name >> age >> high >> weight >> score;
    Stu s1(score,name,age,high,weight);

    cout << "------------------------------" << endl;

    cout << "请输入姓名、年龄、身高、体重:" << endl;
    cin >> name >> age >> high >> weight;
    Per p1(name,age,high,weight);

    cout << "------------------------------" << endl;

    Stu s2(s1);

    cout << "------------------------------" << endl;

    Per p2(p1);

    return 0;
}

代码运行效果图:

思维导图:

模拟面试:

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

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值