C,C++/CONCEPT

Day05-04

너래쟁이 2018. 3. 10. 20:34
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
1. public
 
 
 
 
 
#include <iostream>
#include <iomanip>
using namespace std;
 
 
class A {
 int a1 = 1;
protected:
 int b1 = 2;
public:
 int c1 = 3;
 void seta1(int a1) {
  this->a1 = a1;
 }
 int geta1() {
  return a1;
 }
 void setb1(int b1) {
  this->b1 = b1;
 }
 int getb1() {
  return b1;
 }
};
class B :public A {
 
 int a2 = 4;
protected:
 int b2 = 5;
public:
 int c2 = 6;
 void seta2(int a2) {
  this->a2 = a2;
 }
 int geta2() {
  return a2;
 }
 void setb2(int b2) {
  this->b2 = b2;
 }
 int getb2() {
  return b2;
 }
 
};
class C :public B {
 
 
 int a3 = 7;
protected:
 int b3 = 8;
public:
 int c3 = 9;
 void seta3(int a3) {
  this->a3 = a3;
 }
 int geta3() {
  return a3;
 }
 void setb3(int b3) {
  this->b3 = b3;
 }
 int getb3() {
  return b3;
 }
};
 
 
void main() {
 C cc;
 //직접 접근 가능한거 직접표현
 //접근 불가능 함수로 표현
 
 cout << cc.geta1() << endl;
 cout << cc.getb1() << endl;
 cout << cc.c1 << endl;
 cout << cc.geta2() << endl;
 cout << cc.getb2() << endl;
 cout << cc.c2 << endl;
 cout << cc.geta3() << endl;
 cout << cc.getb3() << endl;
 cout << cc.c3 << endl;
 
}
 
 
 
 
 
2. protected
 
#include <iostream>
#include <iomanip>
using namespace std;
 
 
class A {
 int a1=1;
protected:
 int b1=2;
public:
 int c1=3;
 void seta1(int a1) {this->a1 = a1;}
 int geta1() const{return a1;}
 void setb1(int b1) {this->b1 = b1;}
 int getb1() const{return b1;}
 void setc1(int c1) {this->c1 = c1;}
 int getc1() const{return c1;}
 
};
class B :protected A {
 A aa;
 int a2=4;
protected:
 int b2=5;
public:
 int c2=6;
 
 void seta2(int a2) {this->a2 = a2;}
 int geta2() const{return a2;}
 void setb2(int b2) {this->b2 = b2;}
 int getb2() const{return b2;}
 void setc2(int c2) {this->c2 = c2;}
 int getc2() const{return c2;}
 
};
class C :protected B{
 //오버라이드 쓸때는 부모와 자식이 똑같아야함
 
 int a3=7;
protected:
 int b3=8;
public:
 int c3=9;
 
 void seta1(int a1) { A::seta1(a1); }//A꺼에 a1을 불러줘
 int geta1() constreturn A::geta1(); }
 void setb1(int b1) { A::setb1(b1); }
 int getb1() const { return A::getb1(); }
 void setc1(int c1) { A::setc1(c1); }
 int getc1() const { return A::getc1(); }
 
 void seta2(int a2) { B::seta2(a2); }
 int geta2()const { return B::geta2();}
 void setb2(int b2) { B::setb2(b2); }
 int getb2() constreturn B::getb2();}
 void setc2(int c2) { B::setc2(c2); }
 int getc2() constreturn B::getc2();}
 
 
 void seta3(int a3) { this->a3 = a3; }
 int geta3() {return a3;}
 void setb3(int b3) {this->b3 = b3;}
 int getb3() {return b3;}
};
 
 
void main(){
 C cc;
 //직접 접근 가능한거 직접표현
 //접근 불가능한거 함수로 표현
 
 cout << cc.geta1() << endl;
 cout << cc.getb1() << endl;
 cout << cc.getc1() << endl;
 cout << cc.geta2() << endl;
 cout << cc.getb2() << endl;
 cout << cc.getc2() << endl;
 cout << cc.geta3() << endl;
 cout << cc.getb3() << endl;
 cout << cc.c3 << endl;
 
 
}
cs