1. Review the three access control characters
public: Modified member variables or methods, which can be used outside the class or inside the class
Private: Modified member variables or methods, which can only be used inside the class , not outside the class
protected: The modified member variable or method can only be used inside the class or inside the subclass , not outside the class
These three access controllers can modify the member properties of the class, and can also modify the way of inheritance when inheriting
The inheritance of classes
Classes linked by inheritance form a hierarchical relationship, usually there is a base class at the root of the hierarchical relationship, and other classes inherit directly or indirectly from the base class.
Let's first look at an example of inheritance
class A { public: int a; private: int b; protected: int c; public: void print(); }; class B : public A { public: void printB(); };
From the above example, we can see that there are three member variables a, b, and c in class A, which are public, private, and protected, respectively, and a public member function. The way class B inherits class A is public.
Through some inheritance, we can access the members of the base class in the derived class. But not all inheritance methods are accessible, whether it can be accessed is determined by the access controller of the base class and the inheritance method .
Three, the difference between the three inheritance methods
I will first express the difference between the three of them in a table
|
|
original access attribute |
||
following Inherit direction Mode |
|
public |
private |
protected |
public |
public |
private |
protected |
|
private |
private |
private |
private |
|
protected |
protected |
private |
protected |
1. Inherited by public method, the original access attributes remain unchanged
①If the original access attribute is public, then we can access the public members of the base class inside or outside the class
②If the original access attribute is private, then we will not be able to access the private member of the base class
③ If the original access attribute is protected, then we can access the protected member of the base class inside the derived class, but cannot access it outside the class.
2. Inherited by private method, all attributes become private.
①If the original access attribute is public, then we can access it inside the derived class, but can no longer access the members of the base class outside the derived class
② If the original access attribute is private, then we will not be able to access the private members of the base class.
③ If the original access attribute is protected, then we can access the protected member of the base class inside the derived class, but cannot access it outside the class.
3. Inherited by the protected method, the public from the principle becomes protected, and both private and protected remain unchanged
①If the original access attribute is public, then we can access the public members of the base class inside the derived class, but we cannot access it outside the class
② If the original access attribute is private, then we will not be able to access the private members of the base class.
③ If the original access attribute is protected, then we can access the protected member of the base class inside the derived class, but cannot access it outside the class.
[Note] The access mentioned above is a direct access .
If there are mistakes, you can point them out in the comments.