Limitations of Inheritance

Source: Internet
Author: User


There is a general saying that encapsulation, inheritance, and polymorphism are called the three features of object-oriented. If you are familiar with C ++ and have some thoughts on object-oriented thinking, you may have doubts about this statement. In essence, object-oriented thinking considers that the world is composed of objects, unlike the process-oriented world view, the so-called three characteristics have no relationship with the essence of Object-oriented Thinking, the accurate expression should be encapsulation, inheritance, and polymorphism, which are three main features of C ++ compared with C. If you happen to know a little about C ++ compiler, you may find encapsulation, inheritance, and polymorphism are just syntactic sugar, skill-level things, and it has nothing to do with thinking.

The above is nonsense.

 

This article mainly discusses the Inheritance Mechanism of C ++. Many C ++ textbooks like to use some geometric concepts when talking about inheritance, such as modeling the following set relationships:

 


During an internal technical training, I raised this question. As a result, everyone was speechless. So I specifically asked a novice programmer to answer the question. He gave me the expected answer:Take the Quadrilateral as the base class, And the rectangle and square are inherited in sequence.. No one expresses consent and no one expresses opposition. It may be the first reaction of all people to come up with this solution, but the old bird programmer will immediately notice what is wrong, even if he cannot provide a better solution.

In fact, you will never be able to design a class without a given requirement scenario. Let alone design a group of classes and their hierarchies.This problem occurs in many teaching materials. Student and Teacher are designed without saying what functions are to be completed, even after many years of C ++, looking back at those examples is still dizzy, not to mention beginners. Of course, this may be just because I am too stupid.

We set two simple requirements to take the edge length and the calculated area, and implement three classes without considering the inheritance relationship. They look like the following:

	1 class Quadrangle
2 {
3 public:
4     int GetSideLength(int index);
5     int GetArea(void);
6 
7 private:
8     int m_arrSlide[4];
9 };
10 
11 class Rectangle
12 {
13 public:
14     int GetWidth(void);
15     int GetHeight(void);
16     int GetArea(void);
17 
18 private:
19     int m_nWidth;
20     int m_nHeight;
21 };
22 
23 class Square
24 {
25 public:
26     int GetWidth(void);
27     int GetArea(void);
28 
29 private:
30     int m_nWidth;
31 };

Obviously, the implementation of the Square quadrilateral Square is the simplest, and the implementation of the Quadrilateral Quadrangle is the most complex (Do you know that the four edges can determine the unique Quadrilateral ?). The Inheritance Mechanism has one characteristic: the derived class is always more complex than the base class, because the derived class adds new members and methods based on the complete inheritance of the base class implementation. From the quadrilateral to the rectangle and then to the square is getting simpler and simpler, which forms a paradox. As a result, we cannot describe the relationship between the three according to the hierarchy of inheritance.

An old programmer will tell you that it is best to implement three classes separately without considering the relationship between the three classes. This is feasible in most scenarios. If you really need to describe the hierarchy between the three, the best way to think of it is to use the interface:


 

Interface is used to describe the hierarchical relationship. Each class is implemented independently. It can also be seen that although the interfaces in C ++ are implemented using pure virtual class inheritanceThe interface mechanism and the Inheritance Mechanism are two completely different things..

 

So far, we can draw a conclusion:In fact, the Inheritance Mechanism is difficult to describe the hierarchy of realistic concepts, which is its limitation..In many cases, an inherited application is not used to describe the hierarchy of real concepts, but only to organize code. For example, you can try to describe an evolutionary tree with an inheritance relationship, and you will find it very difficult.

 

What is the purpose of introducing an Inheritance Mechanism in C ++? Most of the information is not detailed. However, at least half of the purpose is to organize and reuse code, inheritance extension is a very common method. Such code can be seen everywhere in frameworks such as MFC and WTL. However, in actual applications, we must avoid using inheritance mechanisms simply to reuse code. Typical cases such as windows and controls.

Windows and controls are two completely different things. In order to reuse the message mechanism, Microsoft put the two concepts together, and all the controls are inherited from the window, this directly results in high complexity and difficulty in scaling the GUI framework.

Code reuse is only a by-product of good design and should not be the purpose of the design.

 

The core points of this article are summarized as follows:

1.The Inheritance Mechanism has great limitations and it is difficult to describe the hierarchy of realistic concepts;

2.When using inheritance, avoid setting up the hierarchy of realistic concepts;

3.Avoid using inheritance for the purpose of code reuse

Weibo: @ flying ashtray


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.