-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathdemo30.cpp
92 lines (82 loc) · 1.12 KB
/
demo30.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
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
#include <iostream>
using namespace std;
class A
{
public:
A():a(1),b(2),c(3),d(10),e(20) {}
int a;
int d;
int e;
protected:
int b;
private:
int c;
};
class B1 : public A
{
public:
void test()
{
cout << a << endl;
cout << b << endl;
// cout << c << endl;
}
};
class C1 : public B1
{
public:
void test()
{
cout << a << endl;
cout << b << endl;
}
};
class B2 : private A
{
public:
using A::d; // 将d变成公有的.
void test()
{
cout << a << endl;
cout << b << endl;
// cout << c << endl;
}
};
class B3 : protected A
{
public:
void test()
{
cout << a << endl;
cout << b << endl;
// cout << c << endl;
}
};
class B4 : A // 默认是private继承
{
int x; // 默认的是private
};
struct B5 : A // 默认是public继承
{
int y; // 默认是公有的public
};
int main()
{
B1 b1;
cout << b1.a << endl;
b1.test();
// cout << b1.b << endl;
// cout << b1.c << endl;
B2 b2;
b2.test();
cout << b2.d << endl;
// cout << b2.e << endl;
// cout << b2.a << endl;
// cout << b2.b << endl;
B3 b3;
b3.test();
// cout << b3.a << endl;
// cout << b3.b << endl;
// cout << b3.c << endl;
return 0;
}