"C + + Primer Plus", chapter 14th, code reuse in C + + learning notes

Source: Internet
Author: User
Tags class definition

C + + provides a means of centralizing the reuse of code. The common inheritance introduced in the 13th chapter establishes is-a relationships so that derived classes can reuse the code of the base class. Private inheritance and protection inheritance also make it possible to reuse the code of the base class, and the has-a relationship is established alone. When using private inheritance, the accumulated public and protected members are referred to as private members of derived classes, and when protected inheritance is used, the accumulated public and protected members are referred to as protected members of derived classes. Regardless of which inheritance is used, the common interface of the base class is called the internal interface of the derived class. This is sometimes referred to as an inheritance implementation, but does not inherit the interface because the derived class object cannot explicitly use the base class's interface. Therefore, you cannot treat a derived class object as a base class object. For this reason, a base-class pointer or reference cannot point to a derived object without an explicit type conversion.
You can also reuse class code by developing classes that contain object members. Which hair is called the inclusion, hierarchy, or combination, and it establishes a has-a relationship. The inclusion is easier to implement and use than private inheritance and protection inheritance, so it is limited in this way. However, private inheritance and protection and ratio include a number of different functions. For example, inheritance allows derived classes to access the protected members of the base class, and also allows derived classes to redefine virtual functions inherited from the base class. Because the inclusion is not an inheritance, you cannot use these features when you reuse class code by including it. On the other hand, if you need to use several objects of a class, the inclusion is more appropriate. For example, the State class can contain a set of country objects.
Multiple inheritance (MI) makes it possible to reuse code for multiple classes in a class design. Private mi or protected MI establishes a has-a relationship, while a total MI establishes a is-a relationship. Mi brings up the problem of defining the same name multiple times, inheriting multiple base class objects. You can use class qualifiers to solve problems with name ambiguity, and use virtual base classes to avoid the problem of inheriting multiple base class objects. Once you have used the virtual base class, you need to introduce a new rule for writing the constructor initialization list and resolving the two semantic issues.
Class templates enable the creation of generic class designs, where types (usually member types) are represented by type parameters. A typical template is as follows:
Template <class T
Class ICS
{
T v;
...
Public
Ic (const T & val): V (val) {}
...
};
where T is a type parameter that is used as a placeholder for the actual type that will be specified later (this parameter can be any valid C + + name, which is typically used by T and type). In this environment, you can also use TypeName instead of class:
Template <typename t>//Same as template <class t>
Class Rev {...};
A class definition (instantiation) is generated when declaring a class object and specifying a specific type. For example, the following declaration causes the compiler to generate a class declaration, replacing all of the type parameter T in the template with the actual type short in the declaration:
Class ic<short> sic; Implicit instantiation
Here, the class name is Ic<short&gt, not the IC. Ic<short> is called template materialization. Specifically, this is an implicit instantiation.
When you use the keyword template to declare a specific materialization of a class, an explicit instantiation occurs:
Template class ic<int>; Explicit instantiation
In this case, the compiler will use a generic template to generate an int materialized--ic<int&gt, although the object has not yet been requested by the class.
Explicit materialization can be provided-overriding the template to specify a specific class declaration. The method is preceded by a template<>, followed by the template class name, plus the angle brackets (which contain the type to be materialized). For example, the code for providing a dedicated IC class for a character pointer is as follows:
Template <> class Ic<char *>
{
char * STR;
...
Public
IC (const char * s): Str (s) {}
...
};
Thus, the following declaration would use a private definition for chic instead of a generic template:
Class Ic<char *> chic;
A class template can specify more than one generic type, or it can have non-type parameters:
Template <class T, class TT, int n>
Class Pals {...};
The following declaration generates an implicit instantiation, substituting a double for t, a string instead of a TT, and a 6 instead of N:
Pals<double, String, 6> mix;
A class template can also contain parameters that are itself templates:
Template < template <typename t> class Cl, typename U, int z >
Class Trophy {...};
where z is an int value, U is the type name, CL is a class template that uses Template<typename, t> declaration.
Class templates can be partially materialized:
Template <class t> pals<t, T, 10> {...};
Template <class T, class tt> pals<t, TT, 100> {...};
Template <class T, int n> Pals <t, t*, n> {...};
The first declaration, which has the same two types, and a value of N of 6, creates a materialization. Similarly, the second declaration creates a materialization of the case where n equals 100, and the third declares that the second type is a pointer to the first type, creating a materialization.
Template classes can be used as members of other classes, structs, and templates.
All of these mechanisms are designed to allow programmers to reuse tested code without having to copy them manually. This simplifies the programming effort and provides the reliability of the program.

"C + + Primer Plus", chapter 14th, code reuse in C + + learning notes

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.