多继承以及虚基类
1 /* 多继承以及虚基类 */
2 #include<iostream>
3
4 using namespace std;
5
6
7 // 只有多继承 才产生虚基类
8
9 class base // 虚基类
10 {
11 public:
12 base()
13 {
14 cout << "base create" << endl
15 }
16
17 ~base()
18 {
19 cout << "base delete" << endl
20 }
21
22 int x;
23 };
24
25 class baseA : virtual public base// 虚继承
26 {
27 public:
28 baseA()
29 {
30 cout << "baseA create" << endl
31 }
32
33 ~baseA()
34 {
35 cout << "baseA delete" << endl
36 }
37 };
38
39
40 class baseB : virtual public base
41 {
42 public:
43 baseB()
44 {
45 cout << "baseB create" << endl
46 }
47
48 ~baseB()
49 {
50 cout << "baseB delete" << endl
51 }
52 };
53
54 class baseAB : public baseA,public baseB
55 {
56 public:
57 baseAB()
58 {
59 cout << "baseAB create" << endl
60 }
61
62 ~baseAB()
63 {
64 cout << "baseAB delete" << endl
65 }
66 };
67
68 void main()
69 {
70 //baseA *p = new baseA;
71
72 //baseAB *p = new baseAB;
73 //delete p;
74
75 baseAB *p = new baseAB;
76 //p->x;// 产生二义性
77 p->baseA::x;
78 p->baseB::x;
79
80 delete p;
81
82 cin.get();
83 }
长风破浪会有时,直挂云帆济沧海
posted on 2015-06-09 14:45 Dragon-wuxl 阅读(115) 评论(0) 收藏 举报
浙公网安备 33010602011771号