Access in C ++ derived class (subclass) control

#include <iostream>
using namespace std;

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

//类的内部检验继承访问权限		    继承方式:私有private
/*class Student : private People
{   
public:
    void show()	    //内部继承访问
    {
	//a = 1;    //私有 不可以访问
	b = 2;	    //保护 可以被访问
	c = 3;	    //公有 可以被访问
    }

};*/

/*class Student : protected People	    //继承方式:保护protected
{
public:
    void show()	    //内部继承访问
    {
	//a = 1;    //私有 不可以访问
	b = 2;	    //保护 可以被访问
	c = 3;	    //公有 可以被访问
    }

};*/
class Student : public People		    //继承方式:公有public
{
public:
    void show()	    //内部继承访问
    {
	//a = 1;    //私有 不可以访问
	b = 2;	    //保护 可以被访问
	c = 3;	    //公有 可以被访问
    }

};

//类的外部访问类的继承权限
int main()
{
    //Student s;
    /*//s.a = 1;    //私有 不可以被访问	    外部继承方式:private
    //s.b = 2;	    //保护 不可以被访问
    s.c = 3;	    //公有 不可以被访问*/
    
    /*Student s;
    //s.a = 1;	    //私有 不可以被访问	    外部继承方式:protected
    //s.b = 2;	    //保护 不可以被访问	    
    //s.c = 3;	    //公有 不可以被访问*/
    
    Student s;
    //s.a = 1;	    //私有 不可以被访问	    外部继承方式:public
    //s.b = 2;	    //保护 不可以被访问	    
    s.c = 3;	    //公有 可以被访问

    return 0;
}

Summary of
private inheritance:
Internal: the base class private in a derived class can not be accessed by the base class protection, the public in the derived class can be accessed
externally: the base class private, protected, public can not be accessed in a derived class
protected inheritance
inside : base class private in a derived class can not access the protected base class, the public can be accessed in a derived class
outside: the base class private, protected, public can not be accessed in a derived class
public inheritance:
internal: the base class private access to the base class can not be protected, the public can be accessed in a derived class in the derived class
external: private base class, the base class protection can not be accessed by the public in the derived class can be accessed in a derived class

Guess you like

Origin blog.csdn.net/qq_41915323/article/details/93741956