博客
关于我
【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/

    你可能感兴趣的文章
    响应的HTTP协议格式+常见的响应码
    查看>>
    将windows里的内容直接复制粘贴到ubuntu,提高效率
    查看>>
    [PHP] error_reporting(0)可以屏蔽Fatal error错误
    查看>>
    thinkphp 的一些重要知识点
    查看>>
    Java学习第二章——Java基本语句
    查看>>
    遇到问题之-yum update无法连接镜像问题解决
    查看>>
    pycharm如何设置(错误、警告类的标准提醒)
    查看>>
    Python3运行的时候错误:ModuleNotFoundError: No module named 'PIL'
    查看>>
    PHP是世界上最好的语言?Phython第一个不服
    查看>>
    Bugku CTF-web6
    查看>>
    python入门到秃顶(10):异常
    查看>>
    百度背景换肤案例
    查看>>
    输出对象的值——踩坑
    查看>>
    在苹果Mac上如何更改AirDrop名称?
    查看>>
    541【毕设课设】基于单片机电阻电感电容RLC测量仪系统
    查看>>
    基于8086交通灯系统仿真设计(微机原理设计资料)
    查看>>
    找中位数
    查看>>
    如何将萌推商品主图、属性图、详情图批量保存到电脑的方法
    查看>>
    springboot redis key乱码
    查看>>
    Win10禁用自带的笔记本键盘
    查看>>