类的多继承大致可以分为两种情况.一种是无共同基类的.一种是有共同基类的.
先看一下第一种情况:
1 #include <stdio.h>
2 class xuzhina_dump_c06_s5_mother
3 {
4 private:
5 int m_age;
6 int m_beauty;
7 public:
8 virtual void print()
9 {
10 printf( "mother\n" );
11 }
12
13 virtual void setBeauty( int age, int beauty )
14 {
15 m_age = age - 5;
16 m_beauty = beauty;
17 }
18 };
19
20 class xuzhina_dump_c06_s5_father
21 {
22 private:
23 int m_strong;
24 int m_age;
25 public:
26 virtual void print()
27 {
28 printf( "father\n" );
29 }
30 virtual void setStrong( int strong, int age )
31 {
32 m_strong = strong;
33 m_age = age;
34 }
35 };
36
37 class xuzhina_dump_c06_s5_child: public xuzhina_dump_c06_s5_father,
38 public xuzhina_dump_c06_s5_mother
39
40 {
41 private:
42 bool m_newMind;
43 public:
44 virtual void print()
45 {
46 printf( "child\n" );
47 }
48
49 virtual void setGender( bool gender )
50 {
51 m_newMind = true;
52 if ( gender )
53 {
54 setBeauty( 10, 10 );
55 }
56 else
57 {
58 setStrong( 20,20 );
59 }
60 }
61 };
62
63 int main()
64 {
65 xuzhina_dump_c06_s5_child* child = new xuzhina_dump_c06_s5_child;
66 child->setGender( false );
67 child->print();
68
69 xuzhina_dump_c06_s5_father* f = child;
70 f->print();
71
72 xuzhina_dump_c06_s5_mother* m = child;
73 m->print();
74
75 return 0;
76 }
更多内容请关注微信公众号“debugeeker", 链接为https://mp.weixin.qq.com/s/nHK_EfKd92-psOO06sl8BQ