C + + is-a was-a has-a holds-a

Source: Internet
Author: User

C + + is-a was-a has-a holds-a


Flyfish 2015-1-28


Is-a: Public inheritance generally referred to as two objects is a is-a relationship

Was-a: Private inheritance called two objects is a was-a relationship

Has-a:has-a relationship describes an instance of another type in a class

HOLDS-A: Describes a class that holds a pointer to another type or reference


Is-a: The Liskov substitution principle (Liskov Substitution Principle) makes a more restrictive definition of is-a

A subclass object can be used to replace its base class object. Every thing that applies to base classes must also apply to derived classes,

Because each derived class object is also a base class object.


Where Is-a and was-a are inheritance relationships. Has-a and holds-a are combinatorial relationships.

Reasons to use combinatorial relationships as much as possible

Inheritance makes the design more tightly coupled, and when one class inherits from another, the subclass can gain access to all public and protected members of the base class, regardless of the permission inheritance.

This class is only coupled with the public members of other classes when using composition.

When you use a holds-a relationship, you do not need to refer to the header files of other classes, only the predecessor declaration is used.


C + + is-a was-a has-a holds-a

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.