Inheritance and combinatorial differences of C + + classes

Source: Internet
Author: User

The "Inheritance" feature of C + + can improve the reusability of the program. It is because "inheritance" is too useful, too easy to use, to prevent the chaos of "inheritance." We are going to set some rules for the use of "inheritance":

first, if the class a and B are irrelevant, so B should not be allowed to inherit a in order to make b more functional. the function .

Do not think "do not eat white do not eat", let a healthy young man to eat ginseng for no reason to fill the body.

second, if the class B It is necessary to use The function of a, there are two situations to consider:

(1) If logically B is a "one" (a Kind of), B is allowed to inherit the function of a. If a man is a man (Human), a boy is a kind of man. Then the class man can derive from the class human, and the class boy can derive from the class man. The sample program is as follows:

Class Human
{
...
};
Class Man:public Human
{
...
};
Class Boy:public Mans
{
...
};

(2) If logically A is a "part" of B, bis not allowed to inherit the function of 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 class eyes, Nose, Mouth, ear, not derived. The sample program is as follows:

Class Eye
{
Public
void look (void);
};
Class Nose
{
Public
void smell (void);
};
Class Mouth
{
Public
void Eat (void);
};
Class Ear
{
Public
void Listen (void);
};
The right design, the lengthy procedure
Class Head
{
Public
void look (void) {M_eye. Look (); }
void smell (void) {m_nose. Smell (); }
void Eat (void) {M_mouth. Eat (); }
void Listen (void) {m_ear. Listen (); }
Private
Eye M_eye;
Nose M_nose;
Mouth M_mouth;
Ear m_ear;
};

If you allow head to be derived from eye, Nose, Mouth, Ear, head will automatically have look, smell, Eat, and Listen features:

The wrong design
Class Head:public eye, public Nose, public Mouth, public Ear
{
};

The program is short and running correctly, but the design is wrong. This is the beginning of many programmers who can not afford to "inherit" the temptation to make design mistakes.

Transferred from: http://www.cnblogs.com/BeyondAnyTime/archive/2012/05/20/2510770.html

Inheritance and combinatorial differences of C + + classes

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.