Differences in C + + inheritance and composition

Source: Internet
Author: User

1. What is inheritance a inherits B, which indicates that a is a type of B, and that all behavior of B is meaningful to a eg:a=woman b=HUMANA= Ostrich b=Bird (NO), because the bird can fly, but the ostrich will not. 2. What is a combination if logically a is a "part" of B (a), then B is not allowed to derive from a, but to combine B with a and other things. For example, the eye, Nose (Nose), Mouth (Mouth), ear (ear) are part of the head (heads), so the class head should be composed of a group of eyes, Nose, Mouth, and ears, not derived3Advantages and disadvantages of inheritance: it is easy to implement new implementations because most of them can be inherited.  It is easy to modify or extend those implementations that are reused. Disadvantage: It destroys encapsulation because it exposes the implementation details of the parent class to subclasses.  The "white box" is reused because the inner details of the parent class are usually visible to subclasses.  When the implementation of the parent class changes, the subclass will have to change as well. Implementations that inherit from the parent class will not be able to change during run time. 4Advantages and disadvantages of the combination: the container class can only access it through the interface of the contained object.  The "black box" is reused because the inner details of the contained object are not visible to the outside.  Good encapsulation. The interdependence of the implementations is relatively small.  (less dependency between the contained object and the container object) Each class is focused on only one task. You can dynamically define (an object's) composition during run time by obtaining a reference to another object with the same type.  Disadvantage: Causes too many objects in the system. In order to be able to use several different objects as a combination block (composition block), the interface must be carefully defined. 5. Choice of both is-a relationship with inheritance expression, has-a relationship with a combination of expression inheritance is a concept of specialization and combination is a concept of assembly is also determined to be a combination or inheritance, one of the clearest ways is to ask whether a new class is required to map upward, that is, when we want to reuse the original type as a new type of internal implementation, we'd better set our own, If we want not only to reuse internal implementations but also to reuse interfaces, we use inheritance. 6Rule: Prioritize (object) combinations rather than (class) inheritance

Personal understanding: The combination of coupling degree is small, easy to expand, inheritance coupling is too large, should be preferred to use the combination, rather than inheritance.

Differences in C + + inheritance and composition

Related Article

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.