c++ - 什么是访问说明符?我应该以私有(private)、 protected 还是公共(public)继承?

标签 c++ class private protected

我对访问修饰符在继承方面的含义感到困惑。涉及privateprotectedpublic关键字的继承有什么区别?

最佳答案

什么是访问说明符?

C++ 中的类/结构/union 有 3 个访问说明符。这些访问说明符定义了如何访问类的成员。当然,类的任何成员都可以在该类中访问(在同一类的任何成员函数内)。继续访问访问说明符的类型,它们是:

Public - 声明为 Public 的成员可以通过类的对象从类外部访问。

Protected - 声明为 Protected 的成员只能从类 BUT 外部访问,但只能在从它派生的类中访问。

Private - 这些成员只能从类中访问。不允许外部访问。

源代码示例:

class MyClass
{
    public:
        int a;
    protected:
        int b;
    private:
        int c;
};

int main()
{
    MyClass obj;
    obj.a = 10;     //Allowed
    obj.b = 20;     //Not Allowed, gives compiler error
    obj.c = 30;     //Not Allowed, gives compiler error
}

继承和访问说明符

C++ 中的继承可以是以下类型之一:

  • 私有(private)继承
  • 公共(public)继承
  • protected 继承

以下是与这些相关的成员访问规则:

First and most important rule Private members of a class are never accessible from anywhere except the members of the same class.

公有继承:

All Public members of the Base Class become Public Members of the derived class &
All Protected members of the Base Class become Protected Members of the Derived Class.

即成员的访问权限没有变化。我们之前讨论的访问规则会进一步应用于这些成员。

代码示例:

Class Base
{
    public:
        int a;
    protected:
        int b;
    private:
        int c;
};

class Derived:public Base
{
    void doSomething()
    {
        a = 10;  //Allowed 
        b = 20;  //Allowed
        c = 30;  //Not Allowed, Compiler Error
    }
};

int main()
{
    Derived obj;
    obj.a = 10;  //Allowed
    obj.b = 20;  //Not Allowed, Compiler Error
    obj.c = 30;  //Not Allowed, Compiler Error

}

私有(private)继承:

All Public members of the Base Class become Private Members of the Derived class &
All Protected members of the Base Class become Private Members of the Derived Class.

代码示例:

Class Base
{
    public:
      int a;
    protected:
      int b;
    private:
      int c;
};

class Derived:private Base   //Not mentioning private is OK because for classes it  defaults to private 
{
    void doSomething()
    {
        a = 10;  //Allowed 
        b = 20;  //Allowed
        c = 30;  //Not Allowed, Compiler Error
    }
};

class Derived2:public Derived
{
    void doSomethingMore()
    {
        a = 10;  //Not Allowed, Compiler Error, a is private member of Derived now
        b = 20;  //Not Allowed, Compiler Error, b is private member of Derived now
        c = 30;  //Not Allowed, Compiler Error
    }
};

int main()
{
    Derived obj;
    obj.a = 10;  //Not Allowed, Compiler Error
    obj.b = 20;  //Not Allowed, Compiler Error
    obj.c = 30;  //Not Allowed, Compiler Error

}

protected 继承:

All Public members of the Base Class become Protected Members of the derived class &
All Protected members of the Base Class become Protected Members of the Derived Class.

代码示例:

Class Base
{
    public:
        int a;
    protected:
        int b;
    private:
        int c;
};

class Derived:protected Base  
{
    void doSomething()
    {
        a = 10;  //Allowed 
        b = 20;  //Allowed
        c = 30;  //Not Allowed, Compiler Error
    }
};

class Derived2:public Derived
{
    void doSomethingMore()
    {
        a = 10;  //Allowed, a is protected member inside Derived & Derived2 is public derivation from Derived, a is now protected member of Derived2
        b = 20;  //Allowed, b is protected member inside Derived & Derived2 is public derivation from Derived, b is now protected member of Derived2
        c = 30;  //Not Allowed, Compiler Error
    }
};

int main()
{
    Derived obj;
    obj.a = 10;  //Not Allowed, Compiler Error
    obj.b = 20;  //Not Allowed, Compiler Error
    obj.c = 30;  //Not Allowed, Compiler Error
}

请记住,相同的访问规则适用于继承层次结构中的类和成员。


注意事项:

- 访问规范是每个类而不是每个对象

请注意,访问规范 C++ 是基于每个类而不是基于每个对象的。
一个很好的例子是,在复制构造函数或复制赋值运算符函数中,可以访问正在传递的对象的所有成员。

- 派生类只能访问其自身基类的成员

考虑 following code example :

class Myclass
{ 
    protected: 
       int x; 
}; 

class derived : public Myclass
{
    public: 
        void f( Myclass& obj ) 
        { 
            obj.x = 5; 
        } 
};

int main()
{
    return 0;
}

它给出了一个编译错误:

prog.cpp:4: error: ‘int Myclass::x’ is protected

因为派生类只能访问其自己的基类的成员。请注意,这里传递的对象 obj 与访问它的 derived 类函数没有任何关系,它是一个完全不同的对象,因此是 derived 成员函数不能访问它的成员。


什么是 friend friend 如何影响访问规范规则?

您可以将一个函数或类声明为另一个类的friend。当您这样做时,访问规范规则不适用于 friended 类/函数。类或函数可以访问该特定类的所有成员。

So do friends break Encapsulation?

不,它们没有,相反,它们增强了封装!

friendship 用于表示两个实体之间的有意的强耦合
如果两个实体之间存在特殊关系,即一个实体需要访问其他 privateprotected 成员,但您不希望 everyone 要使用 public 访问说明符进行访问,那么您应该使用 friendship。

关于c++ - 什么是访问说明符?我应该以私有(private)、 protected 还是公共(public)继承?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/5447498/

相关文章:

c++ - 如何从注册表中读取本地化名称?

c++ - 数组/列表及其维度

javascript - 如何在构造函数之外调用 super 构造函数?

c# - 为什么明确写 "private"?

kotlin - 如何从 Kotlin 中的类外部调用类的私有(private)函数

c++ - 绘制凸面缺陷 C++ OpenCV

c++ - 调整双缓冲小部件的大小?

c++ - 位置未处理的异常:Microsoft C++ 异常:内存位置的 std::length_error

python - 实例变量自动修改

java - 如何从方法访问私有(private)变量?