[C ++] C ++ in public, protected, private distinction

First: private, public, protected access range:

private: only its friend functions accessed by the class of functions can not be any other access, object of this class can not access;

protected: this class may be a function of the subclass, as well as functions to access the friend can not be access to objects of this class;

public: may be a function of this class, the subclass function, which function to access the friend, may be accessed by objects of the class.

Second: The method of the class inherits the attribute change:

Use private inheritance, all the parent class become private in subclasses;

Use protected inheritance, protected, and public methods of the parent class becomes protected in a subclass, private method of the same;

Use public inheritance, methods, properties of the parent class does not change.

Three types of access

public: any entity that can be accessed

protected: only allow members of the class and subclass of functions to access

private: only allows access to the functions of the members of the class

Three kinds of inheritance

public inheritance

protect inheritance

private inheritance

Combination results

Base class inheritance subclass

public & public inheritance => public

public & protected inherit => protected

public & private inheritance => private


protected & public inheritance => protected

protected & protected继承 => protected

protected & private inheritance => private


private & public inheritance => subclass does not have access

private & protected inherit => subclass does not have access

private & private inherit => subclass does not have access

 

Results can be seen from the above compositions

1, public inheritance does not change the access rights of the members of the base class

2, private inheritance allows access to all the members of the base class in a subclass becomes private

3, protected inherit the base class become protected members of the public members of the subclass, other members of the same access rights.

4, private members of the base class are not inherited affect the way the subclass has no access forever.

In addition, when using private inheritance, there is another mechanism: allow access.

We already know that, when inherited in a private way in the base class, its public and protected members become private members in a subclass. In some cases, however, a need in the subclass inherits one or more members Restoring access in the base class .

 

C ++ supports two ways to achieve this purpose

A method, using statement, this is the way the C ++ Standard Recommended

The second method used to access the statement in the form of base-class :: member ;, the appropriate position in the subclass to access the declared. (Note, only to restore the original access, but can not increase or decrease access)

Guess you like

Origin www.cnblogs.com/shiyublog/p/10996062.html