C++中的继承方式

59 篇文章 0 订阅
52 篇文章 1 订阅

目录

摘要

1. 公有继承(Public Inheritance)

2. 保护继承(Protected Inheritance)

3. 私有继承(Private Inheritance)

4. 多重继承(Multiple Inheritance)

继承列表的项数


摘要

在 C++ 中,继承是一种让一个类(派生类)从另一个类(基类)获得属性和方法的机制。我们在之前有通过这篇文章有聊到过继承的一些简单知识(C++中的封装,继承和多态-CSDN博客)。下面我们将继续介绍一些在实际项目中常用到的一些继承的方式。

1. 公有继承(Public Inheritance):基类的公有成员和保护成员在派生类中保持其访问权限,公有成员在派生类中仍然是公有的,保护成员在派生类中仍然是保护的。
2. 保护继承(Protected Inheritance):基类的公有成员和保护成员在派生类中都变成保护成员。
3. 私有继承(Private Inheritance):基类的公有成员和保护成员在派生类中都变成私有成员。

此外,C++ 还支持 多重继承(Multiple Inheritance),即一个类可以从多个基类继承。继承列表中的项数理论上没有限制,但过多的继承会导致代码复杂性增加和维护难度上升。

1. 公有继承(Public Inheritance)

在公有继承中,基类的公有成员在派生类中保持为公有成员,保护成员保持为保护成员,而私有成员仍然是私有的。

#include <iostream>
using namespace std;

class Base {
public:
    void publicMethod() { cout << "Base public method" << endl; }
protected:
    void protectedMethod() { cout << "Base protected method" << endl; }
private:
    void privateMethod() { cout << "Base private method" << endl; }
};

class DerivedPublic : public Base {
public:
    void derivedMethod() {
        publicMethod();        // 可以访问
        protectedMethod();     // 可以访问
        // privateMethod();    // 无法访问
    }
};

int main() {
    DerivedPublic obj;
    obj.publicMethod();        // 可以访问
    // obj.protectedMethod();  // 无法访问
    // obj.privateMethod();    // 无法访问
    obj.derivedMethod();
    return 0;
}

2. 保护继承(Protected Inheritance)

在保护继承中,基类的公有成员和保护成员在派生类中都变成保护成员,而私有成员仍然是私有的。

#include <iostream>
using namespace std;

class Base {
public:
    void publicMethod() { cout << "Base public method" << endl; }
protected:
    void protectedMethod() { cout << "Base protected method" << endl; }
private:
    void privateMethod() { cout << "Base private method" << endl; }
};

class DerivedProtected : protected Base {
public:
    void derivedMethod() {
        publicMethod();        // 可以访问
        protectedMethod();     // 可以访问
        // privateMethod();    // 无法访问
    }
};

int main() {
    DerivedProtected obj;
    // obj.publicMethod();     // 无法访问
    // obj.protectedMethod();  // 无法访问
    obj.derivedMethod();
    return 0;
}

3. 私有继承(Private Inheritance)

在私有继承中,基类的公有成员和保护成员在派生类中都变成私有成员,而私有成员仍然是私有的。

#include <iostream>
using namespace std;

class Base {
public:
    void publicMethod() { cout << "Base public method" << endl; }
protected:
    void protectedMethod() { cout << "Base protected method" << endl; }
private:
    void privateMethod() { cout << "Base private method" << endl; }
};

class DerivedPrivate : private Base {
public:
    void derivedMethod() {
        publicMethod();        // 可以访问
        protectedMethod();     // 可以访问
        // privateMethod();    // 无法访问
    }
};

int main() {
    DerivedPrivate obj;
    // obj.publicMethod();     // 无法访问
    // obj.protectedMethod();  // 无法访问
    obj.derivedMethod();
    return 0;
}

4. 多重继承(Multiple Inheritance)

在多重继承中,一个派生类可以从多个基类继承。需要注意的是,多重继承可能会带来名字冲突和菱形继承等问题,需要小心处理。

#include <iostream>
using namespace std;

class Base1 {
public:
    void methodBase1() { cout << "Base1 method" << endl; }
};

class Base2 {
public:
    void methodBase2() { cout << "Base2 method" << endl; }
};

class DerivedMultiple : public Base1, public Base2 {
public:
    void derivedMethod() {
        methodBase1();
        methodBase2();
    }
};

int main() {
    DerivedMultiple obj;
    obj.methodBase1();
    obj.methodBase2();
    obj.derivedMethod();
    return 0;
}

继承列表的项数

理论上,继承列表的项数没有限制 -- 多重继承:

#include <iostream>
using namespace std;

class Base1 {
public:
    void methodBase1() { cout << "Base1 method" << endl; }
};

class Base2 {
public:
    void methodBase2() { cout << "Base2 method" << endl; }
};

class Base3 {
public:
    void methodBase3() { cout << "Base3 method" << endl; }
};

class DerivedMultiple : public Base1, public Base2, public Base3 {
public:
    void derivedMethod() {
        methodBase1();
        methodBase2();
        methodBase3();
    }
};

int main() {
    DerivedMultiple obj;
    obj.methodBase1();
    obj.methodBase2();
    obj.methodBase3();
    obj.derivedMethod();
    return 0;
}
  • 4
    点赞
  • 7
    收藏
    觉得还不错? 一键收藏
  • 打赏
    打赏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包

打赏作者

沉夢志昂丶

你的鼓励将是我创作的最大动力

¥1 ¥2 ¥4 ¥6 ¥10 ¥20
扫码支付:¥1
获取中
扫码支付

您的余额不足,请更换扫码支付或充值

打赏作者

实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值