Chapter 10 Inheritance and Combination
Object is an instance of classes (instance). If the object is more than a house, then the class is the design drawings of the house. So the focus of object-oriented design is the design of the class, not the design of the object.
For C programs, the design isolated class is relatively easy, and it is difficult to correctly design the base class and its distribution. This chapter only discusses the concept of "inheritance" and "combination".
Note that the application hotspot between the current face-to-object technology is COM and CORBA, which exceeds the category of C textbooks, please read COM and CORBA.
10.1 inheritance
If A is a base class, B is a derived class of A, then b will inherit a data and functions. E.g:
Class A
{
PUBLIC:
Void Func1 (Void);
Void func2 (void);
}
Class B: Public A
{
PUBLIC:
Void Func3 (Void);
Void Func4 (Void);
}
Main ()
{
B B;
B.Func1 (); // b inherited from a Func1
B.func2 (); // b inherited from a function FUNC2
B.func3 ();
B.func4 ();
}
This simple sample program illustrates a fact that the "inheritance" feature of C can improve the reuse of the program. Because "inheritance" is too useful, it is too easy to use, it should be prevented from using "inheritance". We should give "inheritance" to have some rules.
l [Rule 10-1-1] If class A and class B are not related, it is not possible to let B inherit a function and properties in order to make B function more. Don't think "White White Don't eat", let a good-end healthy youth to eat people to eat people.
l [Rules 10-1-2] If the "a Kind of) logically B is A, B is allowed to inherit a function and attributes. For example, a man (MAN) is a kind of human (human), the boy (Boy) is a man. Then the class Man can derive from class Human, and class BOY can derive from class Mana.
Class Human
{
...
}
Class Man: Public Human
{
...
}
Class Boy: Public Man
{
...
}
u precautions
[Rule 10-1-2] It looks very simple, but there may be unexpected applications when practical applications, the concept of inheritance is not exactly the same in the program world and the real world.
For example, from a biological point of view, an ostrich is one of the birds (BIRD). It is reasonable that ostrich should be derived from class BIRD. But ostrich can't fly, then ostrich :: fly is something?
Class Bird
{
PUBLIC:
Virtual void fly (void);
...
}
Class Ostrich: Public Bird
{
...
}
For example, from a mathematical perspective, a circle is a special ellipse, which is reasonable that Circle should be derived from class Ellipse. However, the ellipse has a long axis and a short axis. If the circle inherits the long axis and the short axis of the ellipse, do you not draw a snake?
Therefore, more stringent inheritance rules should be: If it is logically b is a "one", all the features and attributes of A are meaningful to B, B is allowed to inherit a function and attributes. 10.2 combination
l [Rule 10-2-1] If the logically A is a "a part of" (a part of), B is not allowed to be born from A, but to combine B using A and other things B.
For example, Eye, Nose, Mouth, Ear (EAR) is part of the head (HEAD), and the class HEAD should be combined by Eye, Nose, Mouth, EAR, not derived. Such as Example 10-2-1.
Class eye {public: void look;}; class nose {public: void smell (void);}; class mouth {public: void eat (void);}; class ear {public: Void Listen (Void); }; // The 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) {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 combined by Eye, Nose, Mouth, EAR
If Head is allowed from Eye, Nose, Mouth, EAR, then Head will automatically have these features of Look, SMELL, EAT, LISTEN. Examples 10-2-2 are very short and operate correctly, but this design method is wrong.
// The function is correct and the code is simple, but the design method is wrong. Class Head: Public Nose, Public Nose, Public Mouth, Public Ear {};
Example 10-2-2 Head derived from Eye, Nose, Mouth, EAR
A rooster pursued a hen who just got the egg, do you know why?
Because the hens underwent the duck egg.
Many programmers have committed the design mistakes. "Run the correct" program is not necessarily a high quality program, which is an example.