-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathdemo35.cpp
70 lines (61 loc) · 1.26 KB
/
demo35.cpp
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
#include <iostream>
using namespace std;
/*
1、基类中的 static 成员,在整个继承层次中只有一个实例。
2、在派生类中访问 static 成员的方法:
a. 基类名::成员名
b. 子类名::成员名
c. 对象.成员名
d. 指针->成员名
5. 成员名
*/
class A
{
public:
static size_t object_count()
{ return 100; }
protected:
static const size_t obj_count = 99;
};
class B : A
{
public:
void f(const B& b, const B* b2)
{
cout << A::obj_count << endl;
cout << B::obj_count << endl;
cout << b.obj_count << endl;
cout << b2->obj_count << endl;
cout << obj_count << endl;
cout << A::object_count() << endl;
cout << B::object_count() << endl;
cout << b.object_count() << endl;
cout << b2->object_count() << endl;
cout << object_count() << endl;
}
};
class C : A
{
public:
void f_c(const C& c, const C* c2)
{
cout << A::obj_count << endl;
cout << C::obj_count << endl;
cout << c.obj_count << endl;
cout << c2->obj_count << endl;
cout << obj_count << endl;
cout << A::object_count() << endl;
cout << C::object_count() << endl;
cout << c.object_count() << endl;
cout << c2->object_count() << endl;
cout << object_count() << endl;
}
};
int main()
{
B b;
b.f(b, &b);
C c;
c.f_c(c, &c);
return 0;
}