High-quality C/C ++ programming (10)

Source: Internet
Author: User

Chapter 3 inheritance and combination of ClassesAn object is an instance of a class ). If the object is compared to a house, the class is the design drawing of the house. So the focus of object-oriented design is class design, rather than object design. For C ++ programs, it is easier to design isolated classes. What is difficult is to correctly design the base classes and Their Derived classes. This chapter only describes the concepts of inheritance and composition. Note: currently, the application hotspots of object-oriented technology are com and CORBA. These contents are beyond the scope of C ++ textbooks. Please read the related comments on COM and CORBA. 10.1 inheritanceIf a is a base class and B is a derived class of A, B inherits data and functions of. Example: Class A {public: void func1 (void); void func2 (void) ;}; Class B: Public A {public: void func3 (void ); void func4 (void) ;}; main () {B; B. func1 (); // B inherits func1 B from. func2 (); // B inherits func2 B from. func3 (); B. func4 ();} the simple example program illustrates the fact that C ++'s "inheritance" feature can improve program reusability. Because "inheritance" is too useful and easy to use, it is necessary to prevent the misuse of "inheritance ". We should set rules for "inherit. L Rule 10-1-1 ]If Class A and Class B are irrelevant, B cannot inherit the functions and attributes of Class A to make B more functional. Do not think that "white food is not white food", so that a good and strong young people can eat ginseng for no reason. L Rule 10-1-2 ]Logically, if B is a kind of a, B is allowed to inherit the functions and attributes of. For example, a man is a human, and a boy is a man. Class man can be derived from class human, and class boy can be derived from class man. Class human {...}; Class man: Public Human {...}; Class boy: public man {...}; U Notes Rule 10-1-2 ]It seems simple, but it may be unexpected in actual application. The concept of inheritance is not exactly the same in the procedural world and the real world. For example, from a biological perspective, ostrich is a type of bird. It is reasonable to say that ostrich can be derived from bird. But the ostrich cannot fly, so ostrich: What is fly? Class bird {public: Virtual void fly (void );...}; Class ostrich: Public bird {...}; For example, from a mathematical point of view, circle is a special ellipse. It is reasonable to say that circle classes can be derived from ellipse classes. However, an elliptic has a long axis and a short axis. If the circle inherits the long and short axes of an elliptic, isn't it superfluous? Therefore, the stricter inheritance rules should be: Logically B Yes And All functions and attributes B is allowed. Inherit . 10.2 combinationL Rule 10-2-1 ]Logically, if A is a part of B, B is not allowed to be born from a, but to combine a with other things. For example, eye, nose, mouth, and ear are part of the head, therefore, class head should be composed of class eye, nose, mouth, and ear, not derived. See the example 10-2-1.
Class eye {
Public: void look (void );};
Class nose {
Public: void smell (void );};
Class mouth {
Public: void eat (void );};
Class ear {
Public: void listen (void );};
// Correct design, although the Code is lengthy. 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 ;};
Example 10-2-1 head is composed of eye, nose, mouth, and ear. If the head can be derived from eye, nose, mouth, or ear, the head will automatically have the functions of look, smell, eat, and listen. The example 10-2-2 is very short and runs correctly, but this design method is incorrect.
// The function is correct and the code is concise, but the design method is incorrect. Class head: Public Eye, public nose, public mouth, public ear {};
Example 10-2-2 head a rooster is derived from eye, nose, mouth, and ear to catch up with a hen that has just been laid. Do you know why? Because the hen has duck eggs. Many programmers cannot withstand the temptation of inheritance and make design mistakes. The program that runs correctly is not necessarily a high-quality program. This is an example.

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.