Terms 40: Through the layering, "has one" or "to achieve"
Make some objects of a class into data members of another class, so that a class is constructed on another class, which is called "layering". E.g:
Class address {...}; // Someone's residence
Class Phonnumber {...};
Class Person {public: ...
Private: string name; // The lower object address address; // 同 同 同 同 同 同 上 同 同 同};;
In this example, the Person class is considered to be the upper layer placed in String, Address, and Phonenumber classes because it contains those types of data. The term "layered" has a lot of synonyms, which are often referred to as: Composition, Containing or embedding.
Terms 35 explain the meaning of public inheritance is "Yes". Correspondingly, the hierarchical meaning is "there is a" or "use ... to be implemented".
The above Person class shows the relationship of "there is a". A Person object "has a" name, address, phone number, and fax number. You can't say, a person "is a" name or a person "is an" address; you have to say, "There is a" name, "has an" address, and so on. Most people have no difficulty in distinguishing these, so confused "Is a" and "there is a" situation is relatively rare.
Slightly a bit trouble is distinguishing "is a" and "implementation with ...". For example, suppose a class template is required to indicate a collection of any object, and no elements are repeated in the collection. In programming, reuse is a better thing, and you may have read the overall introduction of the C standard library in terms 49, then your first reaction must be in the standard library. Set template. Yes, since you can use something written by others, why should I write a new template again?
However, after in-depth research of the SET's help documentation, you will find that the following restrictions below will not meet your program requirements: SET requires elements that are included in it must be completely ordered, ie, for two Elements A and B, must be determined: either a
Specifically, you decided to let your SET template inheritance from List. That is, SET
// SET is erroneously used ListTemplate
At this point, everything seems to be correct, but it is actually not small. As explained in Terms 35, if D "is a" B, all facts set to B are also established. However, the List object can contain repetitive elements, so the value of ž3051 is added twice in List
Because the relationship between these two classes is not "a", it is a mistake to use public inheritance to indicate their relationship. The correct way is to let the Set object "implement" with list objects ":
{PUBLIC: BOOL MEMBER (Const T & ITEM) Const;
Void INSERT (Const T & ITEM); Void Remove (Const T & ITEM);
Int cardinality () const;
Private: list
The member function of the SET can use the list of List and other parts of the Standard Library, so that the code is not difficult to write:
Template
Template
IF (it! = rep.end ()) rep.rase (it);
Template
These functions are simple, so I naturally think of them as inline functions; but before making the final decision, I still review the discussion made by the Terms 33. (In the code above, Find, Begin, End, Push_Back and other functions are part of the standard library basic framework, which can be used to operate such a container template such a List. The overall introduction of the standard library framework See Terms 49 and M35.)
It is worth noting that the interface of the SET class is not complete and minimized (see Terms 18). In terms of integrity, its maximum omission is that it is not possible to loop on the contents of the SET, and this feature is required for many programs (all members in the standard library provide this feature, including SET). Another drawback of SET is that there is no compliance that follows the containers used in the standard library (see Terms 49 and M35), resulting in more difficult to use other parts in the library when using SET.
The interface of the SET has these flaws, but the following cannot be masked: SET has an unconfilled correctness on understanding it and the List. This relationship is not "a" (although the first look is as it is), but "implementation" is implemented by a layering, this relationship is to be proud of the designer.
By the way, when the two classes have been connected by a hierarchy, it is actually established compiled dependencies between two classes. With regard to why do this and how to reduce the trouble of this, see Terms 34.