Permissions C ++ classes and objects of understanding

Permission classes and objects in C ++ 1

Here Insert Picture Description
That is, everyone in the class can access, for the object class, which is the user, it can only access public members, and his inheritance derived class can access protected, this protected is to facilitate access to and derived prevent other users from accessing As for how to access it

Below this is a classic understanding (for access to the class to see more!)
First: access range private, public, protected access label.
private:. Only 2 friend function which is accessed by a function of the class 1.
It can not be any other access, object of this class can not access.

protected:. 1. The function may be in the class, the subclass 2, and 3. its friend access function.
But it can not be accessed by objects of this class.

public:.. 1. The function may be in the class, the subclass 2, 3 which function to access the friend, the object may be accessed by a class 4.

Note: The friend function includes three kinds: to a friend's ordinary non-member functions; set other class member functions of a friend; to all members of the Friends of functions yuan class.

Changes class methods inherited attributes: a second.
private property can not be inherited.
Use private inheritance, the parent class public and protected property in the subclass becomes private;
use protected inheritance, the parent class public and protected property becomes protected in a subclass;
use public inheritance, the parent class public and protected attributes It does not change;

As follows:
public: protected: private:
public Inheritance public protected unavailable
inheritance protected protected protected unavailable
private inheritance private private unavailable

protected and private inheritance can inherit reduce access.

To further understand the difference in three different ways continued visibility terms of its members, discussed below from three different angles.
  For the public to proceed:
  (1) the object of its base class member Visibility:
  public member is visible, others invisible. Here members of the same protection to private members.
  (2) the base class member visibility derived class:
  public members and protected members of visible and invisible private members. Here to protect members of the public to the members.
  (3) base class member visibility derived object:
  public member can be seen, the other members are not visible.
  Therefore, when the public continue, object of a derived class can access public members of the base class; a member of a derived class can access public members of the base class and protected members. Here, we must clearly distinguish between access to the base class member function of the derived class object and the derived class is different.
  For private proceed:
  (1) the base class member objects to its visibility:
  public member is visible, the other members are not visible.
  (2) the base class member visibility derived class:
  public members and protected members are visible, while private members are not visible.
  (3) the base class member visibility derived class object:
  All members are not visible.
  So, to continue in the private, members of the base class can no longer continue only from direct access to the derived class, down.
  For continued protection mode:
  This way to continue with the case of a private way to continue the same. The difference is only for members of the derived class, different members of a base class visibility.
  
  
  
  For the base class's private members, can only be accessed by the base class member functions and friend functions, it can not be accessed by other functions.
  The relationship between the base class and the derived class
  Any class can derive a new class, the derived class can also send another unexpected new class, therefore, base and derived class is a relative term.
  The relationship between the base class and the derived class can be described in the following categories:
  1. concrete derived class is the base class of
  the class hierarchy generally reflects the objective world some real models. In this case, it is easy to see: the base class is an abstract derived class number, and the derived class is a concrete base classes. Its base class extracting common features derived class, the derived class by adding a certain behavior becomes an abstract class of useful types.
  2. The derived class is a continuation of the base class definition
  to define an abstract base class, the base class some operations did not materialize. Then define non-abstract derived class that implements the operation defined in the abstract base class. For example, the case of virtual functions such cases. In this case, the derived class is an implementation of the abstract base class, the base class can be seen as a continuation of the definition. This is also a common method derived class.
  3. The combination of the base class is a derived class of
  the multi continuing more than a derived class has a base class, then the derived class will be a combination of all the base class behavior.
  The method of the derived class itself to distinguish the base class is to add data members and member functions. Therefore, the mechanism will continue to make when creating a new class, just explain the difference between the new class and the existing class, so a lot of the original program code can be reused, so there is a category called "reusable software components" .

Not understanding:

The above mentioned visibility is accessibility. About accessibility there is another way of saying. Such rules, called a derived class objects for access to the base class level access, said access to the derived class derived from the base class for the vertical access.
  General rules are as follows:
  When the public continues, vertical and horizontal access to access public members of the base class is not limited;
  when private continues, vertical and horizontal access to the access can not access public members of the base class;
  protection continues, the vertical with access to the public to continue, for the same level of access to private continues.

Published 44 original articles · won praise 9 · views 3383

Guess you like

Origin blog.csdn.net/puying1/article/details/83684697