博客
关于我
【C++】面向对象的三大特性---继承
阅读量:495 次
发布时间: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/

    你可能感兴趣的文章
    MySQL执行SQL文件出现【Unknown collation ‘utf8mb4_0900_ai_ci‘】的解决方案
    查看>>
    Mysql执行update by id的过程
    查看>>
    mysql执行计划
    查看>>
    MySQL执行计划 EXPLAIN参数
    查看>>
    MySQL执行计划【explain】,看这一篇就够啦!
    查看>>
    Mysql执行计划字段解释
    查看>>
    mysql执行计划怎么看
    查看>>
    MySQL执行计划解读
    查看>>
    mysql执行顺序与索引算法
    查看>>
    mysql批量update优化_Mysql中,21个写SQL的好习惯,你值得拥有呀
    查看>>
    mysql批量update操作时出现锁表
    查看>>
    MYSQL批量UPDATE的两种方式
    查看>>
    mysql批量修改字段名(列名)
    查看>>
    MySQL批量插入数据遇到错误1213的解决方法
    查看>>
    mysql技能梳理
    查看>>
    MySQL报Got an error reading communication packets错
    查看>>
    Mysql报错Can‘t create/write to file ‘/tmp/#sql_3a8_0.MYD‘ (Errcode: 28 - No space left on device)
    查看>>
    MySql报错Deadlock found when trying to get lock; try restarting transaction 的问题解决
    查看>>
    MySQL报错ERROR 1045 (28000): Access denied for user ‘root‘@‘localhost‘
    查看>>
    Mysql报错Packet for query is too large问题解决
    查看>>