基类的静态成员
1 /* 基类的静态成员 */
2
3 #include<iostream>
4
5 using namespace std;
6
7 class base
8 {
9 public:
10 static int num;
11
12 base()
13 {
14 num+=1;
15 }
16
17 ~base()
18 {
19 num-=1;
20 }
21 };
22
23 // 基类定义的静态成员,被所有派生类共享
24 // 起到计数器的作用,自身还有所有派生类对象的个数
25 // 遵循私有 公有 保护的继承机制 访问不同
26 // 可以通过父类 子类对象访问 还可以通过父类 子类类名访问
27 int base::num = 0;// 类的静态成员
28
29 class basenew : public base
30 {
31
32 };
33
34 class basenewx : public basenew
35 {
36
37 };
38
39
40 void main()
41 {
42 basenew *p = new basenew[100];
43
44 base *p1 = new base[40]
45
46 basenewx *p2 = new basenewx[50];
47
48 cout << p->num << endl;// 190
49 cout << p1->num << endl;// 190
50 cout << p2->num << endl;// 190
51
52
53 cin.get();
54 }
长风破浪会有时,直挂云帆济沧海
posted on 2015-06-08 22:00 Dragon-wuxl 阅读(389) 评论(0) 收藏 举报
浙公网安备 33010602011771号