C ++ inheritance and derivation of study notes (a)

I understand it is not necessarily correct, but generally only a beginner, do not want fraught, I hope the reader himself to study and criticize what I wrote wrong place

First, both of the same concept and derivative inherited, but is described in different angles, A succession B, B derivative A. Continue to maintain the original class of property, derive the expanded property. (Hereinafter appear derived classes A, B is the base class)

Not inherit the base class in a derived class members copy the code simple, meaning that B has a private member name, A is not equivalent to the also private in to write a name, but representing an increase of a process, if you a written directly on the name, definition of an object from a direct access, and is the object inherits -> A-> B such a process, the addition of a procedure.

Inheritance can be understood as a privilege given.

Inheritance, in addition to all members of the absorbent structure and destructor.

Two, Three members of the class

Class members access control attributes

The type of public members

  In a statement later in the keyword public, they are class interface with the outside, external functions can access any public data types and functions.

  Objects can be accessed directly.

Private type member

  In a statement later in the keyword private, allowing only functions in this class visit , and any function outside of the class can access.

  You can not directly access the object, if you want to access the public function needs to be defined to operate in the class.

Protection type members

  And private Similarly, the difference in performance when inheritance and derive different effects on the derived class

 

Third, inheritance

No matter the kind of inheritance, all inherited everything, except what is inherited in what kind of way inheritance.

The effects of different inheritance is mainly reflected in:

  • Derived class member access to the base class members
  • By derived class object access to the base class members

Public inheritance (public):

Access control is to assign students familiar with the class can directly access to inherited property, access to the means which members can access

  • Inherited access control
    • public and protected members of the base class: access properties remain unchanged in a derived class; (the equivalent of their own class has the same)
    • Private members of the base class: not directly accessible (for the derived class terms), attention is inherited over, is not no inheritance.
  • access permission
    • Member function of the derived class: direct access to the base class public and protected members, but can not directly access the private members of the base class;
    • By the object of a derived class: You can only access the public member inherited from a base class.

  The object of the derived class for themselves derive new member of reference (b), and for the public base class and protected members can be accessed directly, for private members need to derive a class definition public member functions to call public member functions in the base class, and public member functions of this base class can manipulate its private members.

  

Private inheritance (private) 

  • Inherited access control
    • public and protected members of the base class: private identity are to appear in a derived class; (for the object of a derived class)
    • private members of the base class: not directly accessible.
  • access permission
    • Member function of the derived class: direct access to the base class public and protected members, but can not directly access private members of the base class (need to call the base class interface);
    • Any member of the derived class can not directly access any private inherited, you need to pack one member function (back row).
    • By the object of a derived class: direct access not inherit from any base class members.

  All inherited members are required to write in a derived class member function to operate, if it is the base class public and protected members, member functions can be accessed directly, but if it is the base class private members, call the base class the public member functions (ie, interfaces) to operate.

Protected inheritance (protected)

  • Inherited access control
    • public and protected members of the base class: appear in a derived class with protected identity;
    • private members of the base class: not directly accessible.
  • access permission
    • Member function of the derived class: direct access to the base class public and protected members, but can not directly access the private members of the base class;
    • By the object of a derived class: direct access not inherit from any base class members.
  • Features and role of protected members
    • To establish it in the object's class module, it's the same as the nature of private members.
    • For its derived classes, it's the same as the nature of public members.
    • Not only to achieve data hiding, and convenient inheritance, code reuse.
    • If the derived class has a plurality of base classes, that is, when multiple inheritance, each base class can inherit different ways.

 

Whatever inheritance election

Thing in common: a member function in a derived class can directly access the base class public and protected members, but can not directly access private members of the base class; private any base class members are not directly accessible, we need to base class provides the interface.

Different points: public inheritance, the object of a derived class can access public members of the base class. Other objects and inheritance of a derived class can not access directly ask any inherited members.

 

I do not know the difference between access and control access to or just plain know, but I think the actual programming access to understand and know how to operate on the line.

Guess you like

Origin www.cnblogs.com/working-in-heart/p/12094224.html