博客
关于我
【C++】面向对象的三大特性---继承
阅读量:494 次
发布时间:2019-03-07

本文共 2416 字,大约阅读时间需要 8 分钟。

C++继承及其应用

继承的概念

继承是面向对象编程中解决代码重复问题的重要机制,它允许我们在保持已有类特性的同时,扩展功能并创建新的派生类。这种层次化的复用体现了面向对象设计的逻辑思维过程。

示例

class Person {public:    void SetPerson(const string& name, const string& sex, int age) {        name_ = name;        sex_ = sex;        age_ = age;    }private:    string name_;    string sex_;    int age_;};class Student : public Person {public:    void SetStudent(const string& name, const string& sex, int age, int stuID) {        SetPerson(name, sex, age);        stuID_ = stuID;    }private:    int stuID_;};

继承方式

继承权限

访问限定符决定了继承过程中成员的访问级别:

  • public:所有继承者可以访问。
  • protected:子类和派生类可以访问。
  • private:只有当前类和其派生类可以访问。

继承语法

单独和多继承的实现方式:

// 单继承class student : public person {}// 多继承class A : public B1, public B2 {}

类与结构的区别

  • class的默认继承方式是private,而struct默认是public
  • 结构通常用于不需要继承机制的简单数据结构。

模板参数

template
...

父类与子类

子类必须使用public继承才能访问父类的成员。

对象模型

子类对象可以被视为基类对象的扩展。

赋值规则

  • 子类对象赋值给基类对象不支持。
  • 父类指针或引用可以指向子类对象,不反之亦然。
  • 极度转换会导致运行时错误,应尽量避免。
  • 同名隐藏

    子类覆盖父类成员时,需提前声明:

    // 基类class Base {public:    void TestFunc() {        cout << "Base::TestFunc()" << endl;    }private:    int b_;};// 派生类class Derived : public Base {public:    void TestFunc(int b) {        cout << "Derived::TestFunc(int)" << endl;    }private:    int d_;    char b_;};int main() {    Derived d;    d.TestFunc(10); // 调用派生类函数    d.Base::TestFunc(); // 调用基类函数}

    子类构造函数规则

    • 构造顺序:基类 → 子类。
    • 析构顺序:子类 → 基类。

    构造函数限制

    • 必须明确声明构造函数。
    • 需要匹配父类的构造函数。

    拷贝构造与赋值

    // 基类class Base {public:    Base(int b = 10) : b_(b) {        cout << "Base::Base(int)" << endl;    }    Base(const Base& b) : b_(b.b_) {        cout << "Base::Base(const Base&)" << endl;    }    ~Base() {        cout << "Base::~Base()" << endl;    }    Base& operator=(const Base& b) {        if (this != &b) {            b_ = b.b_;        }        return *this;    }private:    int b_;};// 派生类class Derived : public Base {public:    Derived(int b = 10, int d = 20) : Base(b), d_(d) {        cout << "Derived::Derived(int, int)" << endl;    }    Derived(const Derived& d) : Base(d), d_(d.d_) {        cout << "Derived::Derived(const Derived&)" << endl;    }    Derived& operator=(const Derived& d) {        if (this != &d) {            Base::operator=(d);            d_ = d.d_;        }        return *this;    }    ~Derived() {        cout << "Derived::Derived()" << endl;        // 调用基类析构    }private:    int d_;};

    继承与组合的选择

    • 继承适合扩展现有功能。
    • 组合更适合复用对象而不改变结构。

    继承优缺点

    优点:

    • 灵活性高。缺点:
    • 维护成本高。
    • 封装性较弱。

    组合优缺点

    优点:

    • 封装性强。缺点:
    • 对象数量多。

    在编写代码时,优先考虑组合复用,少用继承。

    转载地址:http://biacz.baihongyu.com/

    你可能感兴趣的文章
    #C8# UVM中的factory机制 #S8.4.3# factory机制创建实例接口
    查看>>
    #C8# UVM中的factory机制 #S8.5# 对factory机制的重载进一步思考
    查看>>
    #Day Day Plan# 《NCB_PCI_Express_Base 5.0.1.0》pdf 译文笔记 模版
    查看>>
    #Linux# Linux系统下如何查看磁盘空间占据情况
    查看>>
    #Linux杂记# grep 查找命令常用选项大全(一)
    查看>>
    #Linux杂记# grep 查找命令常用选项大全(二)
    查看>>
    .exe已停止工作_windows资源管理器已停止工作怎么解决
    查看>>
    7 自动开启网卡_软件测试学习教程——CentOS 7 修改网卡设置
    查看>>
    8位二进制转bcd算法 c语言,二进制转BCD码快速算法 bin to bcd fast code.
    查看>>
    900行c语言贪吃蛇,原生js实现的贪吃蛇网页版游戏完整实例
    查看>>
    ado filter 多条记录_Excel 有了Filter函数VLOOKUP函数要靠边站了
    查看>>
    ado读取多条oracle数据,Oracle ADO数据存取
    查看>>
    anaconda新建python2环境安装不了jupyterlab_anaconda3安装及jupyter环境配置教程(全)...
    查看>>
    android asynctask handler 区别,AsyncTask与Thread+Handler简要分析
    查看>>
    android fastjson漏洞_初识Fastjson漏洞(环境搭建及漏洞复现)
    查看>>
    android pod 组件化_CocoaPods 组件化实践 - 私有Pod
    查看>>
    $CH0201$ 费解的开关
    查看>>
    android进程管理策略,Android进程保活
    查看>>
    arduino蓝牙通讯代码_arduino 联接蓝牙模块
    查看>>
    asp.mvc 4项目发布文件目录结构_如何用SpringBoot(2.3.3版本)快速搭建一个项目?文末有小彩蛋...
    查看>>