Object-oriented programming there is no doubt about the inheritance of classes, and the inheritance of classes is divided into three types: public inheritance, private inheritance and protection Inheritance (protected), different inheritance mechanisms allow derived classes to have different access rights to base class members, while class members and object members of derived classes have different access rights to the base class.
before we begin to introduce the access mechanisms of the derived classes, let's look at the members of the class and the members of the object.
Members of the class
A static member, the static type member of a class , that can access all members of a class and can use the class directly . Property (method) to represent
Member of the object
Non-static members must first be the object of a new class, through the object . Property (method) or this-> Property (method) to represent.
OK, now that you understand the class members and object members, let's talk about the inherited access mechanism of the derived class to the base class.
in the inheritance mechanism of the class, the derived class also inherits the private members of the base class, but does not have access rights, or it can be equivalent to a private member that does not inherit the base class at all, and the private members of the base class only allow the friend function calls of the base class itself or the base class, and none of the other access rights. Understand this point under the face of private members will not repeat.
In addition, objects of derived classes cannot access members of the base class's private and protected types and can access only public members. The following are all access rights based on the class members of the derived class
in public inheritance, the public and protected members of the base class maintain the original access properties in the derived class
in private inheritance, the public and protected members of the base class have access to private in the derived class, that is, only the class members of the derived class are allowed access, and none of the other access rights.
in protection inheritance, the public and protected members of the base class have access to the protected type in the derived class, allowing only class members of derived classes or two-level derived classes to access them.
is an illustration of the above explanations (only for members of the class):
Finally, a simple small example to help digest and absorb
Class a{
Public:
F1 ()
Private:
F2 ()
Protected:
F3 ()
}
Class B:public a{
Public:
F4 ()
}
Class C:private b{}
b Members can access the f1 , f3 , cannot access f2
class B object can access the F1 , you cannot access F2 , F3
c Members can access the f1 , f4 , f3 , cannot access f2
class C object can access the F1 , F4 , you cannot access F2 , F3
About the inheritance of the class, in object-oriented programming is an essential part of the above is my understanding after learning, the text has an incorrect point of view, please correct me.
Inherited member access control mechanisms (C + +)