-
Notifications
You must be signed in to change notification settings - Fork 2
/
Copy patha.cpp
92 lines (75 loc) · 1.3 KB
/
a.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
/*
topic:- accessiblity
*/
#include <iostream>
using namespace std;
class Base
{
int x;
protected:
int y;
public:
void setx(int x)
{
this->x = x;
}
void sety(int y)
{
this->y = y;
}
void show()
{
cout << "x: " << x << " y: " << y << "\n";
}
};
class Derived1 : private Base
{
public:
void ssety(int y)
{
this->y = y;
}
void sshow()
{
cout << "x is not accessible from derived. y: " << y << "\n";
}
};
class Derived2 : protected Base
{
public:
void ssety(int y)
{
this->y = y;
}
void sshow()
{
cout << "x is not accessible from derived. y: " << y << "\n";
}
};
class Derived3 : public Base
{
};
int main()
{
Derived1 derived1;
Derived2 derived2;
Derived3 derived3;
Base base;
base.setx(1);
base.sety(2);
base.show();
derived3.setx(2);
derived3.sety(3);
derived3.show();
//derived2.setx(3); error as protected
//derived2.sety(3); error as protected
//derived2.show(); error as protected
derived2.ssety(3);
derived2.sshow();
//derived1.setx(3); error as private
//derived1.sety(3); error as private
//derived1.show(); error as private
derived1.ssety(3);
derived1.sshow();
return 0;
}