Summary: Inheritance is a very important feature of C , and is one of the three characteristics of OO. I hope to make a simple discussion on this, I can eliminate some confusion. What is inheriting? Inheritance is a method of organizing associated classes, and is a way to integrate data and operational behavior between Hengqi, but also notice that inheritance relationship is a strong coupling relationship. What is the purpose of inheritance? When it comes to inheritance, people will always think of the code reuse. In order not, the code reuse is just a side effect of inheritance. The main purpose of inheritance is to express an external meaningful relationship, which describes two entities in the problem domain. The behavior relationship between. In other words, inheritance is due to the realities of the problem domain, not due to the purpose of the technology within the solution.
What is the obstacle to inherit? Inherited use is not as simple as we think, there are many language characteristics that make a certain obstacle when deciding to inherit. 1. The existence of non-virtual member functions. If we determine a member function in a base class is non-virtual, it means that this function should not be redefined in the derived class. If you are redefined, the result is likely not what you expect, For example: Class a {public: void f () {cout << "A :: f" << endl;}}; class b: public a {public: void f () {cout << "b :: f" << endl;}}; a * pa = new b; PA-> f (); delete pa; here, we may expect PA-> F () to output B :: F, but actually A:: F , Of course, if there is no problem with Virtual, the key is how we can clearly determine that the function should be declared as Virtual? How to make the base class fully predict the various needs of subclasses? There is no doubt that this is a challenge! Perhaps all base class members are declared as Virtual is a simple solution, but this will greatly reduce the performance efficiency of the program. For C of the efficiency, this is a betrayal, C is more I hope that we only declare those functions that need to be redefined as Virtual. 2, excessive protection packages for base classes is a good feature, but the degree of packaging is difficult to master, such as Class A {Private: Class P {...};}; Class B: public a :: p { ...}; Experienced programmers will immediately realize that this is a mistake: I can't get A :: P, because it's private! Of course, you only need to change the private to protected, but the key is what the base class predicts what the child needs to inherit? Like an obstacle, this is also a challenge. Tian Zhen's programmer may think that as long as all members in the base class are documented as public / protected, it is good, but in fact, if our class is released, public / protected members will no longer change, otherwise it will be interrupted by customers. The code, this requires us to package the implementation details for Private, only those who have changed those who need change as public / protected permissions (the virtual function can be declared as Private, this is an exception), but the base class The designer requires so high, it is also very difficult. 3, the modular design in the base class is not more concise, effective, but for the base class, it is not easy to do effectively.
For example, we have one-point lookup tree BStree, defined as follows: Template
Ok, we should modify the base class BStree: Template
2, add an access function in the RBNode class, but there is more troubles than Friend. There are also some other choices to make people's headache, such as excessive variables in the base class, inherited attribute selection, etc. Not finished (to be renewed ...)