Inheritance and derived class (self-summary)

In the derived class:

No matter what kind of inheritance are not directly access the private members of a class inherited

  • Not directly access
  • public
  • protected

And to protect the public in accordance with different inheritance varies, different limits of inheritance is derived object to access the base class .

Within a derived class:

  • No function of the same name
    • Aside from any member can access private members of the base class
  • There are a function of the same name (the same name on the line)
    • The derived class with the same name in the same name of function hides the base class
    • Can be distinguished by class name :: function
    • May cause two heterosexual (inherited function with the same name to the [same level])

protected and private inheritance inherited differences:

  • protected are not inherited no -
  • will not have access to private inheritance
  • Inherit only once is not apparent gap

Type Conversion

Public derived class object (public mode) may be used as an object base class, not conversely.

  • External access interface makes publicly derived base class is unchanged
  • Implicitly derived class objects can be converted into a base class object;
  • You can refer to a derived object to initialize the base class
  • Derived class may be a pointer to a pointer implicitly converted base class

By the base class object names, pointers are only inherited from the base class members

do not redefine an inherited non-virtual function , so that the function of the same name to be overwritten, use more virtual functions to meet the needs of multi-state

A derived class can not inherit the base class members

Default constructor, copy constructor, assignment copy function, and destructor member function is called four special member functions

Constructor

Not inherit the base class constructor, then use the derived class constructor to initialize a unified

provisions ++ 11 C:

  • Available constructor using statement inherits
  • But only initialize inherited from the base class members
  • Syntax:
    • using B::B;

Copy constructors and destructors are not inherited, it will only be called when needed.

A copy constructor derived class object to be transferred a plurality of times, an intermediate will typecast

execution order destructor opposite constructor

Virtual base class (and virtual function somewhat different properties)

Processing the base class virtual base class redundancy problem, dynamic binding handler function virtual function call is to distinguish that function

  • Virtual base class ----> virtual inheritance, with the inheritance of the first modified virtual inheritance, which is a pointer to the class inheritance (forming virtual base class)
  • Each inherited virtual base must be initialized (not normal)
  • Solve the redundancy problem, but you must know the name of the virtual base class to initialize
  • Farthest derived class is generated when the direct call of the virtual base class, a virtual base class initialization only once

Virtual function

  • Virtual table is there, used to store the entry address virtual functions (I doubt there will be a function of the same name into the virtual table, otherwise it is a virtual sign)
  • Virtual functions are dynamically bound
  • Virtual function members also have the nature of non-static member function, the function will inherit the same name covering (not discussed virtual function or ordinary function)
  • If the base class virtual function with the same name, the function unless the type and number of parameters and return types are the same is the virtual function, virtual function or not
  • If the derived class defines a new virtual function, not a base class of the same name, then the base-class pointer or reference to a derived class can not be found by the base class virtual function

Summary: 1 to inherited virtual function is a function of the same name in the function, in order to achieve dynamic binding effect, if there is no function of the same name, then the virtual tape without almost;
virtual destructor are special, not also enter the same virtual table

2. Whether the case is a virtual function or virtual destructor, by a base class pointer or reference only dynamic binding, if the copy directly initialized, then the base class is the base class is called only member

Guess you like

Origin www.cnblogs.com/Liberavi/p/11735138.html