C++ 公共的、受保护的、私人的

C++ 公共的、受保护的、私人的,c++,access-modifiers,C++,Access Modifiers,请看下面的代码: #include <iostream> using namespace std; class A { private: int privatefield; protected: int protectedfield; public: int publicfield; }; class B: private A { private: A a; public: void test() { cout &

请看下面的代码:

#include <iostream>

using namespace std;

class A
{
private:
    int privatefield;
protected:
    int protectedfield;
public:
    int publicfield;
};

class B: private A
{
private:
    A a;
public:
    void test()
    {
        cout << this->publicfield << this->protectedfield << endl;
    }
    void test2()
    {
        cout << a.publicfield << a.protectedfield << endl;
    }
};

int main()
{
    B b;
    b.test();
    b.test2();
    return 0;
}
#包括
使用名称空间std;
甲级
{
私人:
国际私人领域;
受保护的:
int-protectedfield;
公众:
国际公共领域;
};
B类:私人A
{
私人:
A A;
公众:
无效测试()
{

cout publicfield protectedfield此->protectedfield: 这意味着protectedfield现在是它自己的属性,所以它可以访问它

a.protectedfield:
a是B类的成员,此成员具有受保护的protectedfield变量。B不能接触该变量,因为受保护意味着只能从a内部访问。

B只能访问其自身的受保护字段或B类型的其他对象(或可能从B派生,如果它将其视为B-s)

B无权访问同一继承树中任何其他无关对象的受保护字段

苹果无权进入橘子的内部,即使它们都是水果

class Fruit
{
    protected: int sweetness;
};

class Apple: public Fruit
{
    public: Apple() { this->sweetness = 100; }
};

class Orange: public Fruit
{
public:
    void evil_function(Fruit& f)
    {
        f.sweetness = -100;  //doesn't compile!!
    }
};

int main()
{
    Apple apple;
    Orange orange;
    orange.evil_function(apple);
}

让我们把整个代码分成几个小部分。复制并粘贴这两个代码,然后尝试编译

#include <iostream>
using namespace std;
class A
{
private:
    int privatefield;
protected:
    int protectedfield;
public:
    int publicfield;
};

int main()
{
    A a;
    cout<<a.publicfield;
    cout<<a.privatefield;/////not possible ! private data can not be seen by an object of that class
    cout<<a.protectedfield;////again not possible. protected data is like privete data except it can be inherited by another.If inherited as private then they are private,if as protected then protected and if as public then also protected.
}
#包括
使用名称空间std;
甲级
{
私人:
国际私人领域;
受保护的:
int-protectedfield;
公众:
国际公共领域;
};
int main()
{
A A;

coutNo,B不是a的子类。如果您使用公共继承,它会是。这是一个解释它是如何工作的。为什么?私有继承意味着所有继承的字段和方法都将成为私有的,因此它们只能在派生类中访问。+1,因为这看起来像是一个初学者问题,但许多答案或注释都是错误的。我做了一个测试:私有继承或公共继承没有区别。受保护意味着从a和a的子类访问。B是a的子类。@l245c4l:B不是a的子类,因为您使用的是私有继承,而不是公共继承。公共继承意味着“is-a”,私有继承意味着“根据实现”。公共/私有继承i没有对象间访问权限每个对象只向其他对象公开其公共接口(无论类是什么).友谊是颠覆访问说明符的唯一方式。注意:类X是它自己的朋友。所以它的意思是互联网上有一半的解释是错误的?因为他们说子类可以访问超类的受保护成员,这是不真实的。它可以访问这些成员,因为它拥有它们的权利?所以受保护意味着它们可以被复制ied的子类与public相同,但不能从类外访问。对吗?你的答案是正确的,但你根本不需要apple类。(正如你所说,编译器抱怨Orange类中的受保护访问)。只需说:一个桔子不能改变另一个水果的内部结构-即使它也是一个桔子。一个桔子可以访问另一个桔子的受保护内部结构,如果它认为它是桔子。-是的,在这个例子中,如果
evil_function
传递了一个桔子而不是苹果的实例,它仍然会是一个编译错误,因为没有保证始终传递橙色。如果
邪恶函数
接受了
橙色&
,而不是
水果&
,则修改参数的受保护成员是合法的。
#include <iostream>

using namespace std;

class A
{
private:
    int privatefield;
protected:
    int protectedfield;
public:
    int publicfield;
};

class B: private A
{
private:
    A a;
public:
    void test()
    {
        cout << this->publicfield << this->protectedfield << endl;
    }
    void test2()
    {
        cout << a.publicfield << endl;
    }
};
int main()
{
    /*Now B will have both public and protected data as private!!!!
    That means
     B now looks like this class


     Class B
     {  

        private:
        int protectedfield;
        int publicfield;
     }

     As we have discussed private/protected data can not be accessed by object of the class
     so you you can not do things like this

     B b;
     b.protectedfield; or b.publicfield;

     */
    B b;
    b.privatefield;////Error !!!
    b.protectedfield/////error!!!!
}