4.1作业

  1. 对菱形继承给出的代码中每一个类,写一个有参构造函数
  2. 写出下列类的,构造函数(有参、无参),析构函数,拷贝构造函数和拷贝赋值函数

class Father { int *p; const string name; } class Son:public Father { int *age;

     3整理思维导图

#include <iostream>
using namespace std;

class A {
public:
    int a;
    A(int value) : a(value) {} // 添加有参构造函数
};

class B : virtual public A {
public:
    int b;
    B(int valueA, int valueB) : A(valueA), b(valueB) {} 
};

class C : virtual public A {
public:
    int c;
    C(int valueA, int valueC) : A(valueA), c(valueC) {} 
};

// 汇集子类
class D : public B, public C {
public:
    int d;
    D(int valueA, int valueB, int valueC, int valueD)
        : A(valueA), B(valueA, valueB), C(valueA, valueC), d(valueD) {}
};

int main() {
    
    D d1(90, 100, 110, 120); 

    d1.a = 90;
    d1.B::a = 80;
    d1.C::a = 80;

    return 0;
}

#include <iostream>
#include <string>
using namespace std;

class Father
{
    int *p;
    const string name;

public:
    
    Father() : p(new int), name("Default Father") {
        cout << "Father无参构造函数" << endl;
    }

   
    Father(int value, const string& father_name) : p(new int(value)), name(father_name) {
        cout << "Father有参构造函数" << endl;
    }

  
    Father(const Father& other) : p(new int(*other.p)), name(other.name) {
        cout << "Father拷贝构造函数" << endl;
    }

   
    Father& operator=(const Father& other) {
    
        cout << "Father拷贝赋值函数" << endl;
        return *this;
    }

   
    ~Father() {
        cout << "Father析构函数,准备释放空间:" << p << endl;
        delete p;
    }
};

class Son : public Father
{
    int *age;

public:
    
    Son() : Father(), age(new int) {
        cout << "Son无参构造函数" << endl;
    }

   
    Son(int father_value, const string& father_name, int son_age)
        : Father(father_value, father_name), age(new int(son_age)) {
        cout << "Son有参构造函数" << endl;
    }

   
    Son(const Son& other) : Father(other), age(new int(*other.age)) {
        cout << "Son拷贝构造函数" << endl;
    }

    
    Son& operator=(const Son& other) {
       
        cout << "Son拷贝赋值函数" << endl;
        return *this;
    }
   
    ~Son() {
        cout << "Son析构函数,准备释放空间:" << age << endl;
        delete age;
    }
};

int main()
{
    Son s1(10, "Father1", 23);
    Son s2 = s1;
    return 0;
}

相关推荐

  1. 作业40 自定义函数

    2024-04-02 12:04:03       10 阅读

最近更新

  1. TCP协议是安全的吗?

    2024-04-02 12:04:03       16 阅读
  2. 阿里云服务器执行yum,一直下载docker-ce-stable失败

    2024-04-02 12:04:03       16 阅读
  3. 【Python教程】压缩PDF文件大小

    2024-04-02 12:04:03       15 阅读
  4. 通过文章id递归查询所有评论(xml)

    2024-04-02 12:04:03       18 阅读

热门阅读

  1. ElasticSearch 实战:ElasticSearch文档多条件查询

    2024-04-02 12:04:03       11 阅读
  2. FIFO控制器设计——日常学习

    2024-04-02 12:04:03       10 阅读
  3. 关系型数据库与非关系型数据库、Redis数据库

    2024-04-02 12:04:03       17 阅读
  4. vue记事本渲染以及交互

    2024-04-02 12:04:03       15 阅读
  5. docker compose部署项目—踩坑记录

    2024-04-02 12:04:03       13 阅读
  6. Linux中的用户和组管理

    2024-04-02 12:04:03       12 阅读
  7. Go-Gin全局错误处理中间件

    2024-04-02 12:04:03       11 阅读
  8. C++ TCP 服务端和客户端通信的例子

    2024-04-02 12:04:03       12 阅读
  9. 前端 prefetch 和 preload 的区别?

    2024-04-02 12:04:03       14 阅读
  10. Yarn 包管理器入门指南

    2024-04-02 12:04:03       14 阅读
  11. linux定时调度任务

    2024-04-02 12:04:03       12 阅读