forked from pezy/CppPrimer
-
Notifications
You must be signed in to change notification settings - Fork 0
/
ex15_20_Base.h
56 lines (43 loc) · 1.33 KB
/
ex15_20_Base.h
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
/*
=================================================================================
C++ Primer 5th Exercise Answer Source Code
Copyright (C) 2014-2015 github.com/pezy/Cpp-Primer
Base,
Pub_Derv, Priv_Derv, Prot_Derv
Derived_from_Public, Derived_from_Private, Derived_from_Protected
If you have questions, try to connect with me: pezy<[email protected]>
=================================================================================
*/
#ifndef CP5_EX15_20_BASE_H_
#define CP5_EX15_20_BASE_H_
inline namespace EX20 {
class Base {
protected:
int prot_mem;
private:
char priv_mem;
};
class Pub_Derv : public Base {
void memfcn(Base &b) { b = *this; }
int f() { return prot_mem; }
};
class Priv_Derv : private Base {
void memfcn(Base &b) { b = *this; }
int f1() const { return prot_mem; }
};
class Prot_Derv : protected Base {
void memfcn(Base &b) { b = *this; }
int f2() { return prot_mem; }
};
struct Derived_from_Public : public Pub_Derv {
void memfcn(Base &b) { b = *this; }
int use_base() { return prot_mem; }
};
struct Derived_from_Private : public Priv_Derv {
// void memfcn(Base &b) { b = *this; } // error: 'Base' not accessible because 'Priv_Derv' uses 'private' to inherit from 'Base'
};
struct Derived_from_Protected : public Prot_Derv {
void memfcn(Base &b) { b = *this; }
};
}
#endif