I. Access control for a single class
1. Class member access level (public, private, protected)
Public: Decorated data members, member functions are inside the class and can be used outside of the class.
Protected: Decorated data members, member functions are used internally within a class, and are available in inherited subclasses.
Private: Decorated data members, member functions can only be used inside the class and cannot be used outside of the class.
second, different inheritance methods will change the access properties of the inherited member
1) inheritance in C + + affects sub-class's external access properties
Public Inheritance: Parent class members maintain the original access level in subclasses
Private Inheritance: Parent class member becomes private member in child class
Protected inheritance: Public members in the parent class become protected
The protected member in the parent class is still protected
Private member in parent class is still private
2) The private member still exists in the subclass, but it cannot be accessed. Regardless of the way the base class is inherited, derived classes cannot directly use the private members of the base class.
3) C + + Neutron class external access attribute table
|
Parent class member Access level |
Following Underwriting Party Expression |
|
Public |
proteced |
Private |
Public |
Public |
proteced |
Private |
proteced |
proteced |
proteced |
Private |
Private |
Private |
Private |
Private |
Iii. the principle of "three-look"
The external Access property sheet is complex, and here is a summary of the "three-look" principle:
The inheritance in C + + (public, private, protected) affects the sub-class's external access properties
Judge a sentence, can be accessed
1) Look at the call statement, which is written in the inner and outer of subclasses
2) See how subclasses Inherit from parent class (public, private, protected)
3) Look at the access level in the parent class (public, private, protected)
Demo
Inheritance of classes subclass external access properties affect # include <cstdlib> #include <iostream>using namespace Std;class a{private:int A; Protected:int B;public:int C; A () {a = 0;b = 0;c = 0;} void set (int a, int b, int c) {this->a = A;this->b = B;this->c = c;}}; Class B:public a{public:void print () {//cout<< "A =" <<a; errcout<< "b =" <<b; okcout<< "C =" <<endl; Ok}};class c:protected a{public:void print () {//cout<< "A =" <<a; errcout<< "b =" <<b; okcout<< "C =" <<endl; Ok}};class d:private a{public:void print () {//cout<< "A =" <<a; errcout<< "b =" <<b<<endl; okcout<< "C =" <<c<<endl; Ok}};int Main () {A AA; B BB; C cc;d dd;aa.c = 100; OKBB.C = 100; OK//CC.C = 100; Err protected by//DD.C = 100; Erraa.set (1, 2, 3); Okbb.set (10, 20, 30); Ok//cc.set (40, 50, 60); Err//dd.set (70, 80, 90); Errbb.print (); Okcc.print (); Okdd.print (); return 0;}
Iv. principles of access level setting for class members of derived classes
How do I properly use public,protected and private to declare access levels for members?
1. Members who need to be accessed by outsiders are set directly to public
2. Members that can only be accessed in the current class are set to private
3. The access rights of members that can only be accessed in the current class and subclass are set to protected,protected members, between public and private.
Access control for derived classes-principle of "three-look"-Class member access level settings for derived classes