You need to enable JavaScript to run this app.
优惠活动
大模型
产品
解决方案
定价
更多

派生类访问类型疑问:public与protected继承的差异是什么

Core Difference Between Public and Protected Inheritance in C++

Great question! Let’s cut straight to the chase—you’ve already nailed that both inheritance modes let the derived class access the base class’s public and protected members internally. The core difference lies entirely in how those inherited members are exposed to code outside the derived class (and to future subclasses of the derived class).

Here’s the breakdown:

  • Public inheritance (class derived: public base):
    This is the classic "is-a" relationship inheritance. The base class’s public members stay public in the derived class, and protected members stay protected. This means:

    • External code (like main() or unrelated functions) can directly call the base class’s original public members on a derived class object.
    • If you create a subclass of this derived class later, it will inherit the base class’s members with their original access levels (assuming the subclass uses public inheritance too).
  • Protected inheritance (class derived: protected base):
    This is a more encapsulated inheritance mode. The base class’s public and protected members both become protected in the derived class. This changes things for external code:

    • External code cannot directly access any of the inherited base class members (even the original public ones) on a derived class object. Only the derived class’s own member functions, its friends, and its future subclasses can access these members.
    • Any subclass of this derived class will see the original base class members as protected (regardless of the subclass’s inheritance mode), since they’re inherited through the protected layer.

Example to Drive It Home

Let’s look at a quick code snippet that shows the practical difference:

#include <iostream>

class Base {
public:
    void publicBaseFunc() { std::cout << "Base public method\n"; }
protected:
    void protectedBaseFunc() { std::cout << "Base protected method\n"; }
};

// Public inheritance
class DerivedPublic : public Base {
public:
    void useBaseMethods() {
        publicBaseFunc();  // Accessible (still public)
        protectedBaseFunc(); // Accessible (still protected)
    }
};

// Protected inheritance
class DerivedProtected : protected Base {
public:
    void useBaseMethods() {
        publicBaseFunc();  // Accessible (now protected)
        protectedBaseFunc(); // Accessible (still protected)
    }
};

int main() {
    DerivedPublic dp;
    dp.publicBaseFunc(); // ✅ Works! Public member stays public
    // dp.protectedBaseFunc(); // ❌ Error: Protected, can't access externally

    DerivedProtected dpt;
    // dpt.publicBaseFunc(); // ❌ Error! Now protected, external code can't access
    dpt.useBaseMethods(); // ✅ Works: Derived class's own method can access internal members
    return 0;
}

To Sum Up

Public inheritance preserves the base class’s interface for external use, making the derived class a true subtype of the base. Protected inheritance hides the base class’s entire interface from the outside world, only keeping it accessible for the derived class and its own descendants.

内容的提问来源于stack exchange,提问作者Saif Faidi

相关产品推荐
方舟 Agent Plan

超全模态模型 × Harness 升级,最新支持 Deepseek-V4.1-Flash、GLM-5.3 系列、Doubao-Seedream-5.0-pro、Kimi-K3 (部分), 限时 9.9 元起

最近更新时间:2026.05.19 03:35:52