Recently re-picked up the "effective C + +", where the taste is different, recorded here ...
This article records the knowledge points of public inheritance ...
(1) The meaning of public inheritance
This inheritance is represented by the is-a (is a kind of) relationship, that is, all the operations of the base class are applied, the same can be applied to derived classes, in other words, such as the existence of Class A (person) and Class B (student), and Class B inherits from Class A in the public way, So it can be said that the Class B object is also a Class A object, then can be applied to class A (person) operation (such as Eat), can be applied to Class B, if not satisfied with this relationship, you can not use public inheritance, but need to consider other design, to constrain the relationship between the two classes. The above is the meaning of the public inheritance, understand that the meaning can guide, can directly inherit an object from the public in the way of another object!!!
(2) Nature of public inheritance
In the design of the inheritance system, the setting of the function property is often a headache, whether it is set to virtual or non-virtual, and sometimes because of whether a class is set to abstract class, the above problems appear, Indicates that we have not yet figured out the true meaning of these property settings in the inheritance system. Here is also a note of the characteristics of public inheritance itself, that is, the members of the public property in the parent class (member functions and member variables) after the quilt class inherits, the public property is still the public attribute, and so on the private property and the protected property.
Here is an example of the nature of public inheritance:
1 classa{2 Public:3 //... (Omit constructors and destructors)4 Virtual voidPurevirfunc ()Const=0;5 Virtual voidVirfunc ()Const;6 voidNonvirfunc ()Const;7 Private:8 //... (Omit member variable)9};
The above Class A defines three member functions, which represent the functions of three kinds of attributes, namely pure virtual function, non-pure virtual function and non-virtual function, and these three functions have their own behavior when inheriting, which represent the essence of inheritance.
1 classD | Publica{2 Public:3 //... (Omit constructors and destructors)4 Virtual voidPurevirfunc ()Const;5 Virtual voidVirfunc ()Const;6 //for the Nonvirfunc function in Class A, here7 //cannot be overridden8 Private:9 //...Ten};
Briefly describe the behavior of three function inheritance:
1. Pure virtual function-Class A sets Purevirfunc to pure virtual function, indicating that class A is an abstract class, it does not produce a concrete object, it is foolish to generate the object of Class A arbitrarily, but can declare its pointer, with its pointer to the concrete derived class object, To achieve the effect of dynamic binding, so:
----------------------------------------------------
A; Error, cannot generate concrete object for abstract class
A * pa = new B ();//correct
Pa->virfunc (); Dynamic binding, called a function of Class B
----------------------------------------------------
Class A sets Purevirfunc as a pure virtual function to tell each seat class (inheriting from Class A in public) that it simply declares the function interface (subclasses can only inherit the declaration of the interface), and how to implement that is the subclass's own business. Of course, C + + also gives a convenience to the eventful abstract class, which provides a definition of the display for pure virtual functions, but because subclasses must override pure virtual functions (otherwise, subclasses are abstract class), so it appears that The abstract base class does not seem necessary to provide a display definition for pure virtual functions, because subclasses do not use them at all. It is true that, in addition to being cool, it is generally not used, however, "effective C + +" illustrates a situation where it is necessary to provide a display-defined behavior for pure virtual functions.
2. Non-pure virtual function-compared to pure virtual function, the general virtual function is more commonly used, after all, most of the objects are specific, it should be able to let them produce objects; When the parent class assigns a function to impure virtual, the default implementation is usually included, which corresponds to the Virfunc function in the example above. That is, to inform its subclass, the function corresponding to the behavior, it provides the function interface declaration, and attach its default implementation, if the default implementation can complete a subclass of the required functionality, then the subclass directly inherit without rewriting the function, otherwise, you need to rewrite the function to meet their own needs. In most cases, subclasses are less satisfied with the default implementations provided by the parent class, overriding the inherited impure virtual functions to reflect polymorphism in the inheritance system.
3. Non-virtual functions-if the parent class defines a behavior as a non-virtual function, the case corresponds to the Nonvirfunc function in the example above, which informs each seat class that it provides the function interface declaration and the necessary implementation of the behavior, and that the implementation of the function satisfies the requirements of the subclass (even if other changes occur later), It does not want subclasses to be self-righteous in rewriting such functions. Conversely, if the implementation provided by the parent does not meet the future requirements of the subclass, then it should be declared as a virtual function instead of a non-virtual function, right?
C + + Learning _1