Two relationships between classes and classes------New standard C + + programming

Source: Internet
Author: User

In C + +, there are two basic relationships between classes and classes: Composite relationships and inheritance relationships.

A composite relationship is also known as a "has a" or "has" relationship, and is manifested as a enclosing class, where one class takes an object of another class as a member variable.

An inheritance relationship is also known as a "is a" relationship or a "yes" relationship, that is, a derived class object is also a base class object.

When designing two related classes, it is important to note that not two classes have common ground and can make them an inheritance relationship. To have Class B inherit Class A, it must satisfy the proposition that what Class B represents is also the thing represented by Class A, which is logically established. For example, write a point on a plane dot:

Class cpoint{    double X, y;};

Also write a circle class ccircle. The Ccircle class has a center point, and the center of the circle is a bit on the plane, so the Ccircle class and the CPoint class seem to have common member variables. If you therefore let the Ccircle class derive from the CPoint class, use the following notation:

Class Ccircle:public cpoint{    double radius;};

is not correct. Because, "The circle is also point" this proposition is not tenable. The correct approach is to use the "has a" relationship, in which the CPoint member variable is introduced in the Ccircle class, representing the center of the circle:

Class ccircle{    CPoint Center;    double radius;};

  

New standard C + + programming

Two relationships between classes and classes------New standard C + + programming

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.