forked from jzplp/Cpp-Primer-Answer
-
Notifications
You must be signed in to change notification settings - Fork 0
/
15.20.cpp
67 lines (57 loc) · 1.09 KB
/
15.20.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
#include <iostream>
class Base
{
public:
void pub_mem();
void memfcn(Base &b) { b = *this; }
protected:
int prot_mem;
private:
char priv_mem;
};
struct Pub_Derv : public Base
{
int f() { return prot_mem; }
//char g() { return priv_mem; }
void memfcn(Base &b) { b = *this; }
};
struct Priv_Derv : private Base
{
int f1() const { return prot_mem; }
void memfcn(Base &b) { b = *this; }
};
struct Prot_Derv : protected Base
{
int f() const { return prot_mem; }
void memfcn(Base &b) { b = *this; }
};
struct Derived_from_Public : public Pub_Derv
{
int use_base() { return prot_mem; }
void memfcn(Base &b) { b = *this; }
};
struct Derived_from_Private : public Priv_Derv
{
//int use_base() { return prot_mem; }
//void memfcn(Base &b) { b = *this; }
};
struct Derived_from_Protected : public Prot_Derv
{
int use_base() { return prot_mem; }
void memfcn(Base &b) { b = *this; }
};
int main()
{
Pub_Derv d1;
Priv_Derv d2;
Prot_Derv d3;
Derived_from_Public dd1;
Derived_from_Private dd2;
Derived_from_Protected dd3;
Base *p = &d1;
//p = &d2;
p = &dd1;
//p = &dd2;
//p = &dd3;
return 0;
}