C++ 继承 派生类的拷贝构造

news2025/7/12 16:04:24
  • 继承
    • 派生类的拷贝构造
    • 构造顺序
    • 拷贝构造
      • 引例1: 当子类,不自实现拷贝构造时,默认调用父类的拷贝构造
      • 引例2: 子类自实现拷贝构造,不做特殊处理时,只会调用父类的构造器.
      • 引例3: 显示的调用父类的拷贝构造器。
      • 案例:
    • 内嵌函数的拷贝构造
      • 引例1 :当内嵌子对象,子类不自实现拷贝构造时,默认调用内嵌子对象的拷贝构造
      • 引例2: 子类自实现的拷贝构造,不做特殊处理,此时只会调用内嵌对象的构造器,此时失去了内嵌对象的拷贝构造的意义
      • 引例3: 显示的调用内嵌对象的拷贝构造器
      • 案例:

继承

派生类的拷贝构造

拷贝构造,也是一种构造函数,也没有被继承下来。

语法:

派生类::派生类(const 派生类& another):基类(another),派生类新成员(another.新成员)
{
    //派生类新成员(another.新成员)
}

构造顺序

在这里插入图片描述

父类中,一部分成员需要拷贝构造来完成,

子类中,也有一部成员需要拷贝构造来完成。

子类中的内嵌子对象也需要拷贝构造来完成

拷贝构造

引例1: 当子类,不自实现拷贝构造时,默认调用父类的拷贝构造

#include <iostream>
using namespace std;


//当子类,不自实现拷贝构造时,默认调用父类的拷贝构造
class A{
    public:
        A(){
            cout << " A ()" << endl;
        }
        A(const A& other){
            cout << " A (const A& other)" << endl;
        }

};

class B: public A{
public:
    B(){
        cout << " B ()" << endl;
    }

    int b;
};

int main() {

    B b;
    B bb(b);
    
    return 0;
}
输出:
 A ()
 B ()
 A (const A& other)

引例2: 子类自实现拷贝构造,不做特殊处理时,只会调用父类的构造器.

#include <iostream>
using namespace std;

//子类自实现拷贝构造,不做特殊处理时,只会调用父类的构造器.
class A{
    public:
        A(){
            cout << " A ()" << endl;
        }
        A(const A& other){
            cout << " A (const A& other)" << endl;
        }

};

class B: public A{
public:
    B(){
        cout << " B ()" << endl;
    }
    B(const B& other){
        cout << " B (const A& other)" << endl;
    }


    int b;
};


int main() {

    B b;
    B bb(b);
    
    return 0;
}
输出:
 A ()
 B ()
 A ()
 B (const A& other)

引例3: 显示的调用父类的拷贝构造器。

#include <iostream>
using namespace std;


//当子类,不自实现拷贝构造时,默认调用父类的拷贝构造
//子类自实现拷贝构造,不做特殊处理时,只会调用父类的构造器.
class A{
    public:
        A(int x){
            a=x;
            cout << " A ()" << endl;
        }
        A(const A& other){
            a=other.a;
            cout << " A (const A& other)" << endl;
        }
        int a;
};

class B: public A{
public:
    B(int x,int b):A(x){
        this->b=b;
        cout << " B ()" << endl;
    }
    
    //显示的调用父类的拷贝构造器。     //子类对象赋值给父类引用,赋值兼容
    B(const B& other):A(other){
        b=other.b;
        cout << " B (const A& other)" << endl;
    }


    int b;
};


int main() {
    B b(200,100);
    B bb(b);
    cout<<b.a<<endl;
    cout<<b.b<<endl;
    return 0;
}

案例:

# include <iostream>
using namespace std;

class Birthday{
private:
    int year_;
    int month_;
    int day_;


public:
    Birthday(int year=0, int month=0, int day=0): year_(year), month_(month), day_(day){}
    void Birthdayprint(){
        cout <<"Birthday:"<< year_ << "/" << month_ << "/" << day_ << endl;
    }

};

class Student{
private:
    Birthday birthday_;
    string name_;
    char sex_;
    float score_;

public:
    Student(string name, char sex, float score , int year, int month, int day): name_(name), sex_(sex), score_(score),birthday_(year, month, day){}
    Student(const Student& other){
        cout<<" Student (const Student& other)"<<endl;
        this->name_ = other.name_;
        this->sex_ = other.sex_;
        this->score_ = other.score_;
    }

    void print(){
        cout << "Name: " << name_ << endl;
        cout << "Sex: " << sex_ << endl;
        cout << "Score: " << score_ << endl;
        birthday_.Birthdayprint();
    }
};


class Graduate: public Student{
private:
    float salary_;

public:
    Graduate(string name, char sex, float score, float salary, int year, int month, int day): Student(name, sex, score, year, month, day), salary_(salary){}

    Graduate(const Graduate& other):Student(other){
        cout<<" Graduate (const Graduate& other)"<<endl;
        this->salary_ = other.salary_;
    }
    void pri(){
        print();
        cout << "Salary: " << salary_ << endl;
    }
};

class Doctor: public Graduate{
private:
    string title_;
public:
    Doctor(string name, char sex, float score, float salary, int year, int month, int day, string title): Graduate(name, sex, score, salary, year, month, day), title_(title){}
    Doctor(const Doctor& other):Graduate(other){
        cout << " Doctor (const Doctor& other)" << endl;
        this->title_ = other.title_;
    }


    void Dpri(){
        pri();
        cout << "Title: " << title_ << endl;
    }
};


int main(){

    Graduate g("Alice", 'F', 95.5, 5000 , 2000, 1, 1);
    g.pri();
    cout <<"==============="<< endl;
    Doctor d("Bob", 'M', 85.0, 4000, 2000, 1, 1, "Doctor");
    d.Dpri();

    cout <<"==============="<< endl;


    Doctor d2(d);
    d2.Dpri();

    return 0;
}


内嵌函数的拷贝构造

引例1 :当内嵌子对象,子类不自实现拷贝构造时,默认调用内嵌子对象的拷贝构造

#include <iostream>
using namespace std;
//todo 内嵌子对象

class C{
public:
    
    C(){
        cout << " C ()" << endl;
    }
    C(const C& other){
        cout << " C (const C& another)" << endl;
    }

};

class A{
public:
    A(int x){
        a=x;
        cout << " A ()" << endl;
    }

    A(const A& other){
        a=other.a;
        cout << " A (const A& other)" << endl;
    }
    int a;
};

class B: public A{
public:
    B(int x,int b):A(x){
        this->b=b;
        cout << " B ()" << endl;
    }
    //当内嵌子对象,子类不自实现拷贝构造时,默认调用内嵌子对象的拷贝构造
    int b;
    C c;
};


int main() {

    B b(200,100);
    B bb(b);


    return 0;
}
输出:
A ()
 C ()
 B ()
 A (const A& other)
 C (const C& another)

引例2: 子类自实现的拷贝构造,不做特殊处理,此时只会调用内嵌对象的构造器,此时失去了内嵌对象的拷贝构造的意义


#include <iostream>
using namespace std;
//todo 内嵌子对象

class C{
public:
    C(){
        cout << " C ()" << endl;
    }
    C(const C& other){
        cout << " C (const C& another)" << endl;
    }

};

class A{
public:
    A(int x){
        a=x;
        cout << " A ()" << endl;
    }

    A(const A& other){
        a=other.a;
        cout << " A (const A& other)" << endl;
    }
    int a;
};

class B: public A{
public:
    B(int x,int b):A(x){
        this->b=b;
        cout << " B ()" << endl;
    }
    //子类自实现的拷贝构造,不做特殊处理,此时只会调用内嵌对象的构造器,此时失去了内嵌对象的拷贝构造的意义
    B(const B& other):A(other){
        b=other.b;
        cout << " B (const A& other)" << endl;
    }


    int b;
    C c;
};


int main() {

    B b(200,100);
    B bb(b);


    return 0;
}


输出:
 A ()
 C ()
 B ()
 A (const A& other)
 C ()
 B (const A& other)

引例3: 显示的调用内嵌对象的拷贝构造器


#include <iostream>
using namespace std;
//todo 内嵌子对象

class C{
public:
    //当内嵌子对象,
    C(){
        cout << " C ()" << endl;
    }
    C(const C& other){
        cout << " C (const C& another)" << endl;
    }

};

class A{
public:
    A(int x){
        a=x;
        cout << " A ()" << endl;
    }

    A(const A& other){
        a=other.a;
        cout << " A (const A& other)" << endl;
    }
    int a;
};

class B: public A{
public:
    B(int x,int b):A(x){
        this->b=b;
        cout << " B ()" << endl;
    }
    //显示调用内嵌子对象的拷贝构造
    B(const B& other):A(other),c(other.c){
        b=other.b;
        cout << " B (const A& other)" << endl;
    }
    int b;
    C c;
};


int main() {

    B b(200,100);
    B bb(b);
    return 0;
}

输出:
 A ()
 C ()
 B ()
 A (const A& other)
 C (const C& another)
 B (const A& other)

案例:

# include <iostream>
using namespace std;

class Birthday{
private:
    int year_;
    int month_;
    int day_;


public:
    Birthday(int year=0, int month=0, int day=0): year_(year), month_(month), day_(day){}
    Birthday(const Birthday& other){
        cout<<" Birthday (const Birthday& other)"<<endl;
        this->year_ = other.year_;
        this->month_ = other.month_;
        this->day_ = other.day_;
    }
    void Birthdayprint(){
        cout <<"Birthday:"<< year_ << "/" << month_ << "/" << day_ << endl;
    }
};

class Student{
private:

    string name_;
    char sex_;
    float score_;
    Birthday birthday_;
public:
    Student(string name, char sex, float score , int year, int month, int day): name_(name), sex_(sex), score_(score),birthday_(year, month, day){}
    Student(const Student& other):birthday_(other.birthday_){
        cout<<" Student (const Student& other)"<<endl;
        this->name_ = other.name_;
        this->sex_ = other.sex_;
        this->score_ = other.score_;
    }

    void print(){
        birthday_.Birthdayprint();
        cout << "Name: " << name_ << endl;
        cout << "Sex: " << sex_ << endl;
        cout << "Score: " << score_ << endl;
    }
};


class Graduate: public Student{
private:
    float salary_;
public:
    Graduate(string name, char sex, float score, float salary, int year, int month, int day):Student(name, sex, score, year,  month,  day), salary_(salary){}

    Graduate(const Graduate& other):Student(other){
        cout<<" Graduate (const Graduate& other)"<<endl;
        this->salary_ = other.salary_;
    }
    void pri(){
        print();
        cout << "Salary: " << salary_ << endl;
    }
};

class Doctor: public Graduate{
private:
    string title_;
public:
    Doctor(string name, char sex, float score, float salary, int year, int month, int day, string title): Graduate(name, sex, score, salary, year, month, day), title_(title){}
    Doctor(const Doctor& other):Graduate(other){
        cout << " Doctor (const Doctor& other)" << endl;
        this->title_ = other.title_;
    }
    void Dpri(){
        pri();
        cout << "Title: " << title_ << endl;
    }
};


int main(){

    Graduate g("Alice", 'F', 95.5, 5000 , 2000, 1, 1);
    g.pri();
    cout <<"==============="<< endl;
    Doctor d("Bob", 'M', 85.0, 4000, 2111, 1, 1, "Doctor");
    d.Dpri();

    cout <<"==============="<< endl;


    Doctor d2(d);
    d2.Dpri();

    return 0;
}

输出Birthday:2000/1/1
Name: Alice
Sex: F
Score: 95.5
Salary: 5000
===============
Birthday:2111/1/1
Name: Bob
Sex: M
Score: 85
Salary: 4000
Title: Doctor
===============
 Birthday (const Birthday& other)
 Student (const Student& other)
 Graduate (const Graduate& other)
 Doctor (const Doctor& other)
Birthday:2111/1/1
Name: Bob
Sex: M
Score: 85
Salary: 4000
Title: Doctor

本文来自互联网用户投稿,该文观点仅代表作者本人,不代表本站立场。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如若转载,请注明出处:http://www.coloradmin.cn/o/1978667.html

如若内容造成侵权/违法违规/事实不符,请联系多彩编程网进行投诉反馈,一经查实,立即删除!

相关文章

Netty二

Netty 问题分析 bootstrap serverBootstrap pipeline和channelPipeline EventLoopGroup和实现类NioEventLoopGroup

U2net论文复现-简单解读-以及奇奇怪怪的改进-测试roc以及pr

论文地址&#xff1a;U2net论文地址 显著性目标检测&#xff1a; Salient ObjectDetetion(SOD)显著性目标检测&#xff0c;就是要把图片中最显著的物体分割出来&#xff0c;所以是二分类任务&#xff0c;只需要背景和前景。 1、Introduce 1.1、目前存在的2个挑战&#xff1…

Day-11 员工管理案例 增删改查、配置文件

SpringBootWeb案例 前面我们已经实现了员工信息的条件分页查询以及删除操作。 关于员工管理的功能&#xff0c;还有两个需要实现&#xff1a; 新增员工修改员工 首先我们先完成"新增员工"的功能开发&#xff0c;再完成"修改员工"的功能开发。而在"新…

Midjourney、Sora和硅谷机密-《分析模式》漫谈15

DDD领域驱动设计批评文集 做强化自测题获得“软件方法建模师”称号 《软件方法》各章合集 “Analysis Patterns”的Preface&#xff08;前言&#xff09;有这么一句&#xff1a; Kent Beck, Ward Cunningham, and Jim Coplein encouraged me to get involved with the commu…

《Advanced RAG》-02-揭开 PDF 解析的神秘面纱

摘要 PDF 文件是非结构化文档的代表&#xff0c;但从 PDF 文档中提取信息是一个具有挑战性的过程。 PDF 文件由一系列指令组成&#xff0c;这些指令指示 PDF 阅读器或打印机在屏幕或纸张上显示符号的位置和方式。与 HTML 和 docx 等文件格式不同&#xff0c;后者使用标记来组织…

8月2日,贪心-买卖股票的最佳时期

大家好呀&#xff0c;今天我们看两道用贪心算法解的两道题&#xff0c;150道经典面试题中的买卖股票的最佳时期1和2。 1.买卖股票的最佳时期1 . - 力扣&#xff08;LeetCode&#xff09; 思路 对于这题&#xff0c;我们其实很容易想出暴力解&#xff0c;那就是从后往前找值&…

【文件系统】抽象磁盘的存储结构 CHS寻址法 | sector数组 | LAB数组

目录 1.为什么要抽象 2.逻辑抽象_版本1 2.1sector数组 ​2.2index转化CHS 3.逻辑抽象_版本2 3.1LBA数组 3.2LAB下标转化sector下标 文件其实就是在磁盘中占有几个扇区的问题❗文件是很多个sector的数组下标❗文件是有很多块构成的❗❗文件由很多扇区构成------>文件…

C语言刷题小记3

题目1 序列中删除指定数字 分析&#xff1a;本题要求我们来删除一个序列中指定的数字&#xff0c;这里大家要注意我们要删除的数字可能不止出现一次&#xff0c;所以我们需要用两个变量来进行处理&#xff0c;一个变量来遍历数组&#xff0c;一个变量来存储数组的位置&#xff…

H5 上使用腾讯位置服务选择收货信息

效果图 首先需要在腾讯位置服务上申请你自己的key&#xff0c;可参考地图选点组件 // 点击打开地图 backurl 是点击选中的位置点后&#xff0c;页面跳转至要返回的地址&#xff08;backurl&#xff09;&#xff0c;会将位置信息添加到回跳地址&#xff08;backurl&#xff0…

【威胁情报】新的 BingoMod Android 安卓恶意软件伪装成安全应用程序,清除数据

关注公众号网络研究观获取更多内容。 小心 BingoMod&#xff01;这种危险的 Android 恶意软件会窃取您的钱财、清除您的手机数据并控制您的设备。 了解如何保护自己免受这种阴险威胁。保持在线安全&#xff01; 计算机安全解决方案提供商 Cleafy 发现了一种狡猾的远程访问木…

PCIe总线-RK3588 PCIe RC初始化流程分析(十二)

1.简介 RK3588 PCIe RC的初始化涉及PCIe设备枚举、中断&#xff08;INTx、MSI、MSI-X&#xff09;配置、BAR配置、ATU配置、链路训练等&#xff0c;下面一一介绍。 2.初始化 当RC的模式为RK_PCIE_EP_TYPE时&#xff0c;平台驱动调用rk_add_pcie_port函数初始化RC&#xff0c…

如何将Maven子项目插入到Maven父项目中

Maven项目的融合具体方法&#xff1a; 1.在电脑本地磁盘拷贝Maven子项目&#xff1b; 2.用IDEA软件打开Maven父项目&#xff1b; 3.在IDEA中选中Maven父项目&#xff1b; 4.将复制好的的Maven子项目粘贴到Maven父项目中&#xff1b; 5.选中子项目的pom文件&#xff0c;右键选择…

apk反编译修改教程系列-----修改apk 解除软件限制功能 实例操作步骤解析_5【二十四】

解除apk功能 限制主要是一些app只有付费或者开通vip才可以使用所有功能。这些对于热爱反编译的你是不是比较愤慨,今天继续以一款app为大家来演示如何去除软件的限制功能。教程的目的主要是学习反编译的基础修改方法,了解app的修改步骤以及基础的入门修改常识。 反编译工具:…

无人机无人车固态锂电池技术详解

随着无人机和无人车技术的飞速发展&#xff0c;对高性能、高安全性电池的需求日益迫切。固态锂电池作为下一代电池技术的代表&#xff0c;正逐步从实验室走向市场&#xff0c;为无人机和无人车等应用领域带来革命性的变化。相比传统液态锂电池&#xff0c;固态锂电池在能量密度…

C++中的二维数组

引言 C语言的二维数组可直接用【】【】建立&#xff0c;C的数组更多实用vector<int>表示&#xff0c;那二维数组如何表示呢&#xff1f; 表示法 解读 vector<int>的含义是。 申请了一个连续空间vector&#xff0c;里面的数据是一个个的int vector<vector&l…

Spring - 解析 统一数据格式返回以及统一异常处理

接上篇文章的统一数据格式返回… 文章目录 1. 统一异常处理1.1 使用 2. 统一数据返回和统一异处理是怎么实现的2.1 initHandleAdapters2.2 initHandleExceptionResolvers 1. 统一异常处理 1.1 使用 统一异常处理的两个关键的注解是ControllerAdvice ExceptionHandler Contro…

C++入门基本语法(2)

一、引用 1、基本概念与定义 引用不是新定义一个变量&#xff0c;而是给已存在的变量起一个别名&#xff0c;编译器不会为引用变量开辟内存空间&#xff0c;它和它所引用的变量公用同一块内存空间&#xff1b; 引用的写法&#xff1a;变量类型& 引用别名 变量&#xff…

第六周:机器学习

目录 摘要 Abstract 一、深度学习的优化算法 1、SGD 2、SGDM 3、Adagrad 4、RMSProp 5、Adam算法 二、分类器 三、卷积神经网络 总结 摘要 接着上周学习率在训练中的影响&#xff0c;本周对深度学习常见的几种优化算法做了总结&#xff0c;着重分析Adam算法的优缺…

太阳光强光照射实验在材料科学中的应用

强光照射实验方法 所谓的强光照射即使用人造太阳光模拟器设备模拟太阳光的真实光照环境。强光照射实验是一种在材料科学中常用的实验方法&#xff0c;主要用于研究材料在强烈光照条件下的稳定性、性能变化及其内在机制。实验通常涉及将材料置于特定波长和强度的光源下&#xff…

【vulnhub】DerpNStink靶机

靶机安装 下载地址&#xff1a;DerpNStink: 1 ~ VulnHub 信息收集 靶机IP扫描 nmap 192.168.93.0/24 端口扫描&#xff0c;开放21、22、80端口 nmap -A 192.168.93.158 -p- 目录扫描 dirsearch -u http://192.168.93.158 进行网址访问&#xff0c;页面上只有个单词DeRPn…