C++day3作业

#include <iostream>

using namespace std;

class Person
{
    int *age;
    string &name;
public:
//    Person()
//    {

//    }
    Person(int a,string &b):age(new int(a)),name(b)
    {
        cout << "Person的有参构造" << endl;
    }
    Person(const Person &other):age(new int(*(other.age))),name(other.name)
    {
        cout << "Person的拷贝构造函数" << endl;
    }
    ~Person()
    {
        delete age;
        cout << "Person的析构函数" << endl;
    }
};

class Stu
{
    double *score;
    Person p1;
public:
//    Stu():
//    {
//    }
    Stu(double score,int a,string b):score(new double(score)),p1(a,b)
    {
        cout << "Stu的有参构造" << endl;
    }
    ~Stu()
    {
        delete score;
        cout << "Stu的析构函数" << endl;
    }
    Stu(const Stu &other):score(new double (*(other.score))),p1(other.p1)
    {
        cout << "Stu的拷贝构造函数" << endl;
    }
//    Stu &operator=(const Stu& other)
//    {
//        this->score=other.score;
//        this->p1 = other.p1;
//    }
    void show();
};

void Stu::show()
{
    cout << "score= " << score << endl;
    cout << "*score= " << *score << endl;

//    cout << "age= " <<  age << endl;
//    cout << "*age= " << *age << endl;
//    cout << "name= " << name << endl;
}




int main()
{
    Stu stu(100.00,18,"zhangsan");
    stu.show();
    cout << "---------------------------------------" << endl;
    Stu stu1 = stu;
    stu1.show();
    cout << "---------------------------------------" << endl;
//    Stu stu2;
//    stu2 = stu;

    return 0;
}

效果图:

相关推荐

  1. QT<span style='color:red;'>作业</span><span style='color:red;'>3</span>

    QT作业3

    2023-12-29 08:44:03      37 阅读
  2. 2.<span style='color:red;'>3</span><span style='color:red;'>作业</span>

    2.3作业

    2023-12-29 08:44:03      27 阅读
  3. QT<span style='color:red;'>3</span><span style='color:red;'>作业</span>

    QT3作业

    2023-12-29 08:44:03      25 阅读
  4. <span style='color:red;'>3</span>.6<span style='color:red;'>作业</span>

    3.6作业

    2023-12-29 08:44:03      27 阅读

最近更新

  1. TCP协议是安全的吗?

    2023-12-29 08:44:03       19 阅读
  2. 阿里云服务器执行yum,一直下载docker-ce-stable失败

    2023-12-29 08:44:03       19 阅读
  3. 【Python教程】压缩PDF文件大小

    2023-12-29 08:44:03       19 阅读
  4. 通过文章id递归查询所有评论(xml)

    2023-12-29 08:44:03       20 阅读

热门阅读

  1. c# 读取文件方式及对比

    2023-12-29 08:44:03       30 阅读
  2. uniapp中globaldata的使用

    2023-12-29 08:44:03       36 阅读
  3. 单例模式(C++)

    2023-12-29 08:44:03       33 阅读
  4. 单例模式的双重检查锁定是什么?

    2023-12-29 08:44:03       31 阅读
  5. 创建型--单例模式

    2023-12-29 08:44:03       42 阅读
  6. C#判断骨龄与生活年龄的比较

    2023-12-29 08:44:03       34 阅读
  7. [云原生] Go web工作流程

    2023-12-29 08:44:03       42 阅读
  8. react的render什么时候渲染?

    2023-12-29 08:44:03       35 阅读
  9. Linux安装Python3.12.0

    2023-12-29 08:44:03       34 阅读
  10. 表情串转换

    2023-12-29 08:44:03       40 阅读
  11. Ubuntu 2x.04 编译FFmpeg 脚本

    2023-12-29 08:44:03       38 阅读
  12. 高阶组件和高阶函数是什么

    2023-12-29 08:44:03       36 阅读