For the private member of the base class, regardless of the derived type, the derived class and derived users have no right to access;
For the principles of protection, the derived class can be accessed regardless of the derived type, but the derived type of user is not entitled;
For public members of the base class, according to the derived type, divided into three types:
Private derived: After the inheritance, it becomes a private member of the derived class, and the derived class can be accessed, but the derived type of user cannot be accessed;
Protection derived: After the inheritance, it becomes the protected member of the school, and the derived class can be accessed, but the derived type of user cannot be accessed;
Public sexy: public members of derived class, derived and derived types of users can be accessed;
Detective classes can be accessed: accessed in a member function of the derived class, that is, in the members of the derived class can be used;
Users of derived class: that is, the object defined derived class, or uses the function of the party;
The user who derives class cannot access: "You can access the symbol". "You cannot access its members; that is, you cannot access it outside the (derived) class;
Detecting the inheritance rights and access rights, summarizing the following two tables:
Base class
Derive type
Properties in derived class
Can you visit in derived class?
Can derive objects access?
Private
Private
Privately born
Unaccessible
Can not access
Can not access
Protected
Private
Accessible
Can not access
Public
Private
Accessible
Can not access
Private
Protected
Deserve
Unaccessible
Can not access
Can not access
Protected
Protected
Accessible
Can not access
Public
Protected
Accessible
Can not access
Private
Public
Publicity
Unaccessible
Can not access
Can not access
Protected
Protected
Accessible
Can not access
Public
Public
Accessible
Accessible
Base class
Derive type
Properties in derived class
Can you visit in derived class?
Can derive objects access?
Private
Private
Unaccessible
Can not access
Can not access
Protected
Unaccessible
Can not access
Can not access
Public
Unaccessible
Can not access
Can not access
Protected
Private
Private
Accessible
Can not access
Protected
Protected
Accessible
Can not access
Public
Protected
Accessible
Can not access
Public
Private
Private
Accessible
Can not access
Protected
Protected
Accessible
Can not access
Public
Public
Accessible
Accessible
Detective classes cannot access private members of the base class, to access the interface that must use the base class, that is, through its member function.
To access directly, there are two ways:
Increase the protection segment (Protected), put the base class private member provides the partial access to the protected segment; the derived class is similar to the inheritance of the base class's protection member, if the public is born, then The derived class is also protected; if it is privately derived, it will change to a private member in the derived class.
A derived class member function that will be required to access the base class is declared as the base class.
============================================================================================================================================================================================================= ================== Example 1: Public inheritance
/ ** /
/ ************************************************** ********************************************************************************************************************* *********************************************************** ******* /
/ ** /
//
//
Base class
Class
CBASE
...
{Private: int x; void func1 () ... {x = 100;} protected: int y; void func2 () ... {y = 200;} public: int z; public: void func3 (); CBASE ();
; CBASE :: CBase ()
...
{X = 10; y = 20; z = 30;}
Void
CBASE :: func3 ()
...
{Z = 300;}
/ ** /
////
//
Detective class (public inheritance -public)
Class
CDERIVE:
public
CBASE
...
{private: int b; public: int C; public: cderive (); void func ();
CDERIVE :: CDerive ()
...
{A = 40; b = 50; c = 60;
Void
CDERIVE :: func ()
...
{// x = 1000; // canNot Access Private MEMBER DECLARED IN CBASE 'Y = 2000; Z = 3000; CBase (); // There is no error in this call, but does not match data of objects of objects Modify // func1 (); // canNot Access Private MEMBER DECLARED IN CLASS 'CBASE' FUNC2 (); func3 ();}
/ ** /
//
//
Main function
Void
Main ()
...
{CBase Obj1; Cderive Obj2; //obj2.x=10000; // Cannot Access Private Member Declared In Class' CBase '//obj2.y=20000; // Cannot Access Protected MEMBER DECLARED IN CBASE' OBJ2.Z = 30000; //obj2.a=40000; // cannot access private member declared in class 'cderive' //obj2.b=50000; // cannot access protected member declared in class 'cderive' obj2.c = 60000; / /// can2.func1 (); // cannot Access Private Member '//obj2.func2 (); // cannot Access Private Member DECLARED IN CBASE' OBJ2.FUNC3 (); Obj2.func () :} 2: Protection inheritance
/ ** /
/ ************************************************** **************************************************************************************************************************** *********************************************************** ******* /
/ ** /
//
//
Base class
Class
CBASE
...
{Private: int x; void func1 () ... {x = 100;} protected: int y; void func2 () ... {y = 200;} public: int z; public: void func3 (); CBASE ();
; CBASE :: CBase ()
...
{X = 10; y = 20; z = 30;}
Void
CBASE :: func3 ()
...
{Z = 300;}
/ ** /
////
//
Detective class (protection inheritance --Protected)
Class
CDERIVE:
protected
CBASE
...
{private: int b; public: int C; public: cderive (); void func ();
CDERIVE :: CDerive ()
...
{// CBase (); // correct strip, but do not have to use // CDerive :: CBase (); // Error call, 'cbease' is not a member of 'cderive' a = 40; b = 50; c = 60;
Void
CDERIVE :: func ()
...
{// x = 1000; Cannot Access Private MEMBER DECLARED IN CLASS 'CBASE' Y = 2000; z = 3000; // CBase (); // There is no error in the call, but does not match the data of the object of the object Modify // func1 (); // canNot Access Private MEMBER DECLARED IN CLAS 'CBASE' FUNC2 (); FUNC3 ();} / ** /
//
//
Main function
Void
Main ()
...
{CBase Obj1; Cderive Obj2; //obj2.x=10000; // canNot Access Private Member Declared In Class' CBase '//obj2.y=20000; // Cannot Access Protected MEMBER DECLARED IN CBASE' // OBJ2 .z = 30000; // canNot Access Public Member Declared In Class 'CBase' //obj2.a=40000; // Cannot Access Private Member Declared In Class 'CDerive' //obj2.b=50000; // Cannot Access protected MEMBER DECLARED IN CLAS 'CDERIVE' OBJ2.C = 60000; //obj2.func1 (); // Cannot Access Private Member Declared In Class 'CBase' //obj2.func2 (); // cannot Access Private Member Declared In Class 'CBase' //obj2.func3 (); // cannot Access Private Member Declared In Class 'CBase' Obj2.func ();
Example 3: Private inheritance
/ ** /
/ ************************************************** *************************************************************************************************************** *********************************************************** ******* /
/ ** /
//
//
Base class
Class
CBASE
...
{Private: int x; void func1 () ... {x = 100;} protected: int y; void func2 () ... {y = 200;} public: int z; public: void func3 (); CBASE ();
; CBASE :: CBase ()
...
{X = 10; y = 20; z = 30;}
Void
CBASE :: func3 ()
...
{Z = 300;}
/ ** /
////
//
Detective class (private inheritance --private)
Class
CDERIVE: CBASE
...
{private: int b; public: int C; public: cderive (); void func ();}; cderive :: cderive ()
...
{// CBase (); // correct strip, but do not have to use // CDerive :: CBase (); // Error call, 'cbease' is not a member of 'cderive' a = 40; b = 50; c = 60;
Void
CDERIVE :: func ()
...
{// x = 1000; // canNot Access Private MEMBER DECLARED IN CBASE 'Y = 2000; Z = 3000; // CBase (); // This call has no error, but will not give the object of the object Data members to modify // func1 (); // canNot Access Private Member Declared In Class 'CBase' FUNC2 (); func3 ();}
/ ** /
//
//
Main function
Void
Main ()
...
{CBase Obj1; Cderive Obj2; //obj2.x=10000; // canNot Access Private Member Declared In Class' CBase '//obj2.y=20000; // Cannot Access Protected MEMBER DECLARED IN CBASE' // OBJ2 .z = 30000; // canNot Access Public Member Declared In Class 'CBase' //obj2.a=40000; // Cannot Access Private Member Declared In Class 'CDerive' //obj2.b=50000; // Cannot Access protected MEMBER DECLARED IN CLAS 'CDERIVE' OBJ2.C = 60000; //obj2.func1 (); // Cannot Access Private Member Declared In Class 'CBase' //obj2.func2 (); // cannot Access Private Member Declared In Class 'CBase' //obj2.func3 (); // cannot Access Private Member Declared In Class 'CBase' Obj2.func ();