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

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


当前回答

为了回答这个问题,我想先用我自己的话描述一下成员的访问器。如果你已经知道这一点,那就跳到标题“下一步:”。

我知道有三种访问器:公共的、受保护的和私有的。

Let:

class Base {
    public:
        int publicMember;
    protected:
        int protectedMember;
    private:
        int privateMember;
};

所有知道Base的都知道Base包含publicMember。 只有子节点(及其子节点)知道Base包含protectedMember。 除了Base,没有人知道privateMember。

“意识到”,我的意思是“承认存在,从而能够获得”。

下一个:

同样的情况也发生在公共、私有和受保护的继承上。让我们考虑一个类Base和一个继承自Base的类Child。

如果继承是公共的,那么所有知道Base和Child的东西也知道Child从Base继承。 如果继承是受保护的,那么只有Child及其子元素知道它们从Base继承。 如果继承是私有的,那么除了Child之外没有人知道继承。

其他回答

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

它来自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
}

它与基类的公共成员如何从派生类中公开有关。

Public——>基类的Public成员为Public(通常为默认值) Protected ->基类的公共成员将受到保护 Private ->基类的public成员为Private

As litb points out, public inheritance is traditional inheritance that you'll see in most programming languages. That is it models an "IS-A" relationship. Private inheritance, something AFAIK peculiar to C++, is an "IMPLEMENTED IN TERMS OF" relationship. That is you want to use the public interface in the derived class, but don't want the user of the derived class to have access to that interface. Many argue that in this case you should aggregate the base class, that is instead of having the base class as a private base, make in a member of derived in order to reuse base class's functionality.

1)公共继承:

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

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

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

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

2)受保护遗产:

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

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

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

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

3)私人继承:

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

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

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

私人:

基类的私有成员只能被该基类的成员访问。

公众:

基类的公共成员可以被该基类的成员、其派生类的成员以及基类和派生类之外的成员访问。

保护:

基类的成员可以访问基类的受保护成员,也可以访问其派生类的成员。


简而言之:

私人:基地

Protected:基础+派生

Public: base + derived +任何其他成员

为了回答这个问题,我想先用我自己的话描述一下成员的访问器。如果你已经知道这一点,那就跳到标题“下一步:”。

我知道有三种访问器:公共的、受保护的和私有的。

Let:

class Base {
    public:
        int publicMember;
    protected:
        int protectedMember;
    private:
        int privateMember;
};

所有知道Base的都知道Base包含publicMember。 只有子节点(及其子节点)知道Base包含protectedMember。 除了Base,没有人知道privateMember。

“意识到”,我的意思是“承认存在,从而能够获得”。

下一个:

同样的情况也发生在公共、私有和受保护的继承上。让我们考虑一个类Base和一个继承自Base的类Child。

如果继承是公共的,那么所有知道Base和Child的东西也知道Child从Base继承。 如果继承是受保护的,那么只有Child及其子元素知道它们从Base继承。 如果继承是私有的,那么除了Child之外没有人知道继承。