본문으로 바로가기

[c++] public / protected / private

category C++ 2018. 3. 21. 02:51

the space coding

◈ public / protected / private

접근 지정자 범위 예시! 

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
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
● public
#include<iostream>
#include<iomanip>
using namespace std;
class A
{
 int a1;//1
protected:
 int b1;//2
public:
 int c1;//3
 void setA1(int n){ a1 = n;}
 void setB1(int n) { b1 = n; }
 int getA1()const { return a1 ; }
 int getB1()const { return b1; }
};
class B:public A
{
 int a2;//4
protected:
 int b2;//5
public:
 int c2;//6
 void setA2(int n) { a2 = n; }
 void setB2(int n) { b2 = n; }
 int getA2()const { return a2; }
 int getB2()const { return b2; }
};
class C:public B
{
 int a3;//7
protected:
 int b3;//8
public:
 int c3;//9
 void setA3(int n) { a3 = n; }
 void setB3(int n) { b3 = n; }
 int getA3()const { return a3; }
 int getB3()const { return b3; }
};
void main()
{
 C cc;
 
 // 접근이 불가능한거 함수로 표현
 cc.setA1(1);
 cc.setB1(2);
 cc.c1 = 3;
 cc.setA2(4);
 cc.setB2(5);
 cc.c2 = 6;
 cc.setA3(7);
 cc.setB3(8);
 cc.c3 = 9;
 
 cout << cc.getA1() << "\n" << endl;
 cout << cc.getB1() << "\n" << endl;
 cout << cc.c1 <<"\n"<< endl;
 cout << cc.getA2() << "\n" << endl;
 cout << cc.getB2() << "\n" << endl;
 cout << cc.c2 <<"\n"<< endl;
 cout << cc.getA3() << "\n" << endl;
 cout << cc.getB3() << "\n" << endl;
 cout << cc.c3 <<"\n"<< endl;
 //직접 적근 가능 한거 직접 표현
 
}
 
 
● private
 
#include<iostream>
#include<iomanip>
using namespace std;
/* privated 거의 안씀
*/
class A
{
 int a1;//1
protected:
 int b1;//2
public:
 int c1;//3
 void setA1(int n) { a1 = n; }
 int getA1() { return a1; }
 void setB1(int n) { b1 = n; }
 int getB1() { return b1; }
 void setC1(int n) { c1 = n; }
 int getC1() { return c1; }
};
class B :private A
{
 int a2;//4
protected:
 int b2;//5
public:
 int c2;//6
 
 void setA1(int n) { A::setA1(n);}
 int getA1() { return A::getA1();}
 void setB1(int n) { A::setB1(n);}
 int getB1() { return A::getB1();}
 void setC1(int n) { A::setB1(n);}
 int getC1() { return A::getA1();}
 
 
 void setA2(int n) { a2=n; }
 int getA2() { return a2; }
 void setC2(int n) { c2 = n; }
 int getB2() { return b2; } 
 void setB2(int n) { b2 = n; }
 int getC2() { return c2; }
 
};
class C :private  B
{
 int a3;//7
protected:
 int b3;//8
public:
 int c3;//9
 //A
 void setA1(int n) { B::setA1(n); }
 int getA1() { return B::getA1(); }
 void setB1(int n) { B::setB1(n); }
 int getB1() { return B::getB1(); }
 void setC1(int n) { B::setC1(n); }
 int getC1() { return B::getC1(); }
 
 //B
 void setA2(int n) { B::setA2(n); }
 int getA2() { return B::getA2(); }
 void setB2(int n) { B::setB2(n); }
 int getB2() { return B::getB2(); }
 
 void setC2(int n) { B::setC2(n); }
 int getC2() { return B::getC2(); }
 
 //C
 void setA3(int n) { a3 = n; }
 int getA3() { return a3; }
 void setB3(int n) { b3 = n; }
 int getB3() { return b3; }
};
void main()
{
 C cc;
 //직접
 cc.setA1(1);
 cout << cc.getA1() << "\n" << endl;
 cc.setB1(2);
 cout << cc.getB1() << "\n" << endl;
 cc.setC1(3);
 cout << cc.getC1() << "\n" << endl;
 cc.setA2(4);
 cout << cc.getA2() << "\n" << endl;
 cc.setB2(5);
 cout << cc.getB2() << "\n" << endl;
 cc.setC2(6);
 cout << cc.getC2() << "\n" << endl;
 cc.setA3(7);
 cout << cc.getA3() << "\n" << endl;
 cc.setB3(8);
 cout << cc.getB3() << "\n" << endl;
 //직접
 cc.c3 = 9;
 cout << cc.c3 << "\n" << endl;
 
 
}
 
 
 
● protected
 
#include<iostream>
#include<iomanip>
using namespace std;
 
class A
{
 int a1;//1
protected:
 int b1;//2
public:
 int c1;//3
 void setA1(int n) { a1 = n; }
 int getA1() { return a1; }
 
 
};
class B :protected A
{
 int a2;//4
protected:
 int b2;//5
public:
 int c2;//6
 void setA2(int n) { a2 = n; }
 int getA2() { return a2; }
 
 
};
class C :protected B
{
 int a3;//7
protected:
 int b3;//8
public:
 int c3;//9
 
 void setA1(int n) { A::setA1(n); }
 void setB1(int n) { b1 = n; }
 void setC1(int n) { c1 = n; }
 int getA1() { return A::getA1(); }
 int getB1() { return b1; }
 int getC1() { return c1; }
 
 void setA2(int n) { B::setA2(n); }
 void setC2(int n) { c2 = n; }
 void setB2(int n) { b2 = n; }
 int getA2() { return B::getA2() ; }
 int getB2() { return b2; }
 int getC2() { return c2; }
 void setA3(int n) { a3 = n; }
 void setB3(int n) { c3 = n; }
 int getA3() { return a3; }
 int getB3() { return c3; }
};
void main()
{
 C cc;
 //직접
 cc.setA1(1);
 cout << cc.getA1() << "\n" << endl;
 cc.setB1(2);
 cout << cc.getB1() << "\n" << endl;
    cc.setC1(3);
 cout << cc.getC1() << "\n" << endl;
 cc.setA2(4);
 cout << cc.getA2() << "\n" << endl;
 cc.setB2(5);
 cout << cc.getB2() << "\n" << endl;
 cc.setC2(6);
 cout << cc.getC2() << "\n" << endl;
 cc.setA3(7);
 cout << cc.getA3() << "\n" << endl;
 cc.setB3(8);
 cout << cc.getB3() << "\n" << endl;
 //직접
 cc.c3=9;
 cout << cc.c3 << "\n" << endl;
 
 
 
}
cs


'C++' 카테고리의 다른 글

[C++]깊은 대입연산자함수  (0) 2018.03.21
[C++] operator 함수  (0) 2018.03.21
스 택 Q  (0) 2018.03.21
◈ 오버로딩이 불가능한 연산자의 종류  (0) 2018.03.21
[java][성적 관리프로그램]  (0) 2018.03.21