c++中公共继承、私有继承和受保护继承之间的区别是什么?

我在SO上找到的所有问题都是针对具体案例的。


当前回答

我发现了一个简单的答案,所以我想把它贴出来,作为我未来的参考。

它来自http://www.learncpp.com/cpp-tutorial/115-inheritance-and-access-specifiers/的链接

class Base
{
public:
    int m_nPublic; // can be accessed by anybody
private:
    int m_nPrivate; // can only be accessed by Base member functions (but not derived classes)
protected:
    int m_nProtected; // can be accessed by Base member functions, or derived classes.
};

class Derived: public Base
{
public:
    Derived()
    {
        // Derived's access to Base members is not influenced by the type of inheritance used,
        // so the following is always true:

        m_nPublic = 1; // allowed: can access public base members from derived class
        m_nPrivate = 2; // not allowed: can not access private base members from derived class
        m_nProtected = 3; // allowed: can access protected base members from derived class
    }
};

int main()
{
    Base cBase;
    cBase.m_nPublic = 1; // allowed: can access public members from outside class
    cBase.m_nPrivate = 2; // not allowed: can not access private members from outside class
    cBase.m_nProtected = 3; // not allowed: can not access protected members from outside class
}

其他回答

公共继承对IS-A关系建模。与

class B {};
class D : public B {};

每个D都是B。

私有继承为IS-IMPLEMENTED-USING关系建模(或者其他什么关系)。与

class B {};
class D : private B {};

D不是B,但每个D都在实现过程中使用了B。私有继承总是可以通过使用包含来消除:

class B {};
class D {
  private: 
    B b_;
};

这个D也可以用B实现,在这种情况下使用它的b_。与继承相比,包含在类型之间的耦合不那么紧密,因此通常应该首选它。有时使用容器代替私有继承不像私有继承那样方便。这通常是懒惰的蹩脚借口。

我想没有人知道什么是受保护的继承模型。至少我还没看到任何令人信服的解释。

我发现了一个简单的答案,所以我想把它贴出来,作为我未来的参考。

它来自http://www.learncpp.com/cpp-tutorial/115-inheritance-and-access-specifiers/的链接

class Base
{
public:
    int m_nPublic; // can be accessed by anybody
private:
    int m_nPrivate; // can only be accessed by Base member functions (but not derived classes)
protected:
    int m_nProtected; // can be accessed by Base member functions, or derived classes.
};

class Derived: public Base
{
public:
    Derived()
    {
        // Derived's access to Base members is not influenced by the type of inheritance used,
        // so the following is always true:

        m_nPublic = 1; // allowed: can access public base members from derived class
        m_nPrivate = 2; // not allowed: can not access private base members from derived class
        m_nProtected = 3; // allowed: can access protected base members from derived class
    }
};

int main()
{
    Base cBase;
    cBase.m_nPublic = 1; // allowed: can access public members from outside class
    cBase.m_nPrivate = 2; // not allowed: can not access private members from outside class
    cBase.m_nProtected = 3; // not allowed: can not access protected members from outside class
}
class A 
{
    public:
       int x;
    protected:
       int y;
    private:
       int z;
};

class B : public A
{
    // x is public
    // y is protected
    // z is not accessible from B
};

class C : protected A
{
    // x is protected
    // y is protected
    // z is not accessible from C
};

class D : private A    // 'private' is default for classes
{
    // x is private
    // y is private
    // z is not accessible from D
};

重要提示:类B, C和D都包含变量x, y和z。这只是访问的问题。

关于受保护继承和私有继承的使用,你可以在这里阅读。

1)公共继承:

a.基类的私有成员在派生类中不可访问。

b.基类的受保护成员在派生类中仍然受保护。

c.基类的公共成员在派生类中保持为公共。

因此,其他类可以通过派生类对象使用基类的公共成员。

2)受保护遗产:

a.基类的私有成员在派生类中不可访问。

b.基类的受保护成员在派生类中仍然受保护。

c.基类的公共成员也成为派生类的受保护成员。

因此,其他类不能通过派生类对象使用基类的公共成员;但它们可用于派生的子类。

3)私人继承:

a.基类的私有成员在派生类中不可访问。

b.基类的Protected & public成员变成派生类的private成员。

因此,基类的成员不能被其他类通过派生类对象访问,因为它们在派生类中是私有的。甚至是Derived的子类 类不能访问它们。

Accessors    | Base Class | Derived Class | World
—————————————+————————————+———————————————+———————
public       |      y     |       y       |   y
—————————————+————————————+———————————————+———————
protected    |      y     |       y       |   n
—————————————+————————————+———————————————+———————
private      |            |               |    
  or         |      y     |       n       |   n
no accessor  |            |               |

y: accessible
n: not accessible

基于这个例子的java…我认为一张小桌子胜过千言万语:)