-
Notifications
You must be signed in to change notification settings - Fork 0
/
tut20.cpp
70 lines (56 loc) · 1.26 KB
/
tut20.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;
class Base
{
protected:
int a = 10; // can only be used for inheritance
public:
int b = 20;
private:
int c = 30;
};
class derived : public Base
{
public:
void showProtected(void)
{
cout << a; // here you can use protected only in this scope , but not in class main
}
};
class derived1 : protected Base
{
public:
void showProtectedAndPublic(void)
{
cout << a << " " << b;
}
}; // all will be taken as protected , even public will be taken as protected and cannot be used in main
// important difference for derivation
// see the main difference of protected or private derivation is that , the protected class can be further inherited but the private class variables cannot be inherited
class derived4 : protected derived1
{
public:
void showProtectedAndPublic(void)
{
cout << a << " " << b;
}
};
class derived2 : private Base
{
public:
void showProtectedAndPublic(void)
{
cout << a << " " << b;
}
};
int main()
{
derived c1;
cout << c1.b; // can only access b here because it's public in base
derived1 c2;
// cout << c2.showProtectedAndPublic();
derived2 c3;
c3.showProtectedAndPublic();
// cout << c2.b;
return 0;
}