Chapter 14th Code reuse in C + +
1. When using public inheritance, a class can inherit an interface and possibly an implementation (a pure virtual function of the base class provides an interface, but does not provide an implementation). The acquisition interface is part of the is-a relationship. In combination, a class can be implemented, but it cannot get an interface. A non-inheriting interface is part of a has-a relationship.
2. C + + has another way to implement has-a relationships-private inheritance. With private inheritance, the public and protected members of the base class are referred to as private members of the derived class. This means that the base class methods will not be called part of the public interface of the derived class object, but they can be used in the member functions of the derived class.
3. Contains adding an object to a class as a named member object, while private inheritance adds the object to the class as an unnamed inherited object. We will use the term sub-object (subobject) to represent the object that is added through inheritance or inclusion.
As a result, private inheritance provides the same attributes and contains the same: Gets the implementation, but does not get the interface. Therefore, private inheritance can also be used to implement has-a relationships.
4. To make a private inheritance, use the keyword private to define the class. In fact, private is the default value, so omitting the access qualifier will also result in private inheritance.
5. Use inclusion or inheritance to establish a has-a relationship
Most C + + programmers tend to include. First, it's easy to understand. The class declaration contains a display named Object that represents the contained class, and the code can refer to those objects by name, and using inheritance will make the relationship more abstract. Second, inheritance can cause many problems, especially when inheriting from multiple base classes, which may have to deal with many problems, such as a separate base class or a separate base class that shares ancestors with a method with the same name. In short, using inclusion is less likely to encounter such trouble. In addition, inclusions can include multiple homogeneous sub-objects. If a class requires 3 string objects, you can use a string member that contains declaration 3 independent. Inheritance can only use one such object (which is difficult to distinguish when the object has no name).
6. In general, you should use inclusions to establish has-a relationships, or you should use private inheritance if the new class needs to access the protected members of the original class, or if you need to redefine the virtual function.
7. When you use protection inheritance, the public and protected members of the base class are referred to as protected members of the derived class. As with private inheritance, the interface of a base class is also available in a derived class, but is not available outside of the inheritance hierarchy. When you derive another class from a derived class, the main differences between private inheritance and protection inheritance are presented. With private inheritance, the third-generation class will not be able to use the interface of the base class, because the public methods of the base class will become private methods in the derived class, and the public and protected methods of the base class will become protected in the second-generation class when using protection inheritance, so that third-generation classes can use them. Protecting inheritance and private inheritance are all representations of has-a relationships.
8. Implicit upward conversion means that you can point a base class pointer or reference to a derived class object without having to make an explicit type conversion.
Public inheritance can be implicitly convertible, and protection inheritance can only be implicitly type-cast in derived classes, and private inheritance cannot be implicitly type-cast.
9. Use the using to redefine access rights
When you use protection derivation or private derivation, the public members of the base class are referred to as protected members or private members. If you want the method of the base class to be available outside the derived class, one method is to define a derived class method that uses the base class method. Another approach is to use a using declaration to indicate a specific base class member that a derived class can use, even if it is a private derivation (note that the using declaration uses only the function name, without parentheses, function feature labels, and return types). P550
10. Multiple-inheritance mi can cause a lot of problems for programmers. The two main problems are: Inherit the same name method from two different base classes, and inherit multiple instances of the same class from two or more related base classes.
11. Virtual base classes allow objects derived from multiple classes (their base classes to be the same) to inherit only one base class object. For example, by using virtual in a class declaration, the worker can be used as the virtual base class for singer and waiter (the order of virtual and public is irrelevant):
Class Singer:virtual public worker{...}
Class Waiter:public virtual worker{...}
You can then define the Singingwaiter class as:
Class Singingwaiter:public Singer, public waiter{...}
New constructor rules for p557-p558 virtual base class
When the base class is virtual, C + + prohibits information from being passed to the base class automatically through intermediate classes to avoid conflicting information. However, the compiler must construct the base class object component before constructing the derived object. If the constructor is not displayed, the compiler calls the default constructor of the virtual base class. If you do not want to construct a virtual base class object through the default constructor, you need to explicitly call the required base class constructor.
P558 which method
Multiple inheritance can cause the two semantics of a function call. You can use scope resolution operators to clarify the intent of the programmer.
Singingwaiter newhire ("Elise Hawks", 2005, 6, soprano);
Newhire. Singer::show ();
However, a better approach is to redefine show () in Singingwaiter and indicate which show () to use.
14. Mixed use of virtual and non-virtual base classes
When a class inherits a particular base class through multiple virtual paths and non-virtual paths, the class contains a base class sub-object that represents all the virtual paths and multiple base-class sub-objects that represent each of the non-virtual paths.
15. The template class starts with Templates <class type> (or template <typename type>), and if you define a class method outside of the class declaration, each function header is also prefixed with template <class Type> and Class qualifiers (STACK<TYPE>::).
16. It is important to know that template classes are not class and member function definitions. They are C + + compiler directives that explain how to generate class and member function definitions. Template member functions cannot be placed in a standalone implementation file. Because templates are not functions, they cannot be compiled separately. The simplest way to do this is to put all the template information in a separate header file and include the header file in the file where you want to use the template.
p574-p575, in a class declaration, you can use a stack, outside of a class, you can use a stack within a template function definition (including formal parameters), specify the return type, or use the scope resolution operator, you must use the full stack<type>.
Template <class T, int n>, T is a type parameter, n is a non-type parameter or an expression parameter. There are some restrictions on expression parameters. An expression argument can be an integer, an enumeration, a reference, or a pointer. Therefore, double m is not legal, double *rm and double *pm are legal. In addition, the template code cannot modify the value of the parameter, nor can it use the address of the parameter. You cannot use an expression such as n++ or &n. Also, when you instantiate a template, the value used as an expression parameter must be a constant expression.
p578-p582 template Versatility (use a template class as a type parameter for a base class, component class, or other template), recursively using a template, using multiple type parameters.
20. Template materialization
Class templates and function templates are similar because they can be implicitly instantiated, explicitly instantiated, and explicitly materialized, collectively referred to as materialization.
1) Implicit instantiation:
Arraytp<int, 100> stuff;
The compiler does not generate an implicit instantiation of the class until it needs the object:
Arraytp<int, 100> *pt;//don't need objects
PT = new Arraytp<double, 30>//requires object
2) Explicit instantiation
Template class Arraytp<string, 100>;
In this case, although the class object is not created or mentioned, the compiler generates a class declaration, including the method definition. As with implicit instantiation, materialization is generated based on the generic template.
3) Explicit materialization
Explicit materialization is the definition of a specific type (used to replace generics in a template).
Template <> class Sortedarray (const char *);
P583 Partial materialization
Generic templates
Template <class T1, class t2> class Pair {...};
materialized T2 to int
Template <class t1> pair<t1, int>{...};
The <> declaration behind the keyword template is a type parameter that is not materialized, and if all is materialized, <> is empty, which results in an explicit materialization.
P584 member templates: declaring template classes or function templates within a template class
P586 using a template as a template parameter
p588-p593 template class and friend
Template class declarations can also have friends. Friends of the template are divided into 3 categories:
Non-template friends, friends themselves are not template functions, but only use the implicit instantiation of the template class as parameters.
Constraint template friend, each materialization of a class obtains a matching friend, and the type of friend depends on the type of materialization. Declare a friend function template outside the class declaration.
Non-constrained template friend, each materialization of a friend is all materialized friends of the class. Declare a friend function template inside a class declaration.
C + + Primer Plus reading notes--14th chapter code reuse in C + +