일 | 월 | 화 | 수 | 목 | 금 | 토 |
---|---|---|---|---|---|---|
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 |
Tags
- border-box
- #android activity
- #C++ has~a
- html plug-in
- html5 new tag
- #다차원포인터
- html charset
- relative path
- 하이퍼레저패브릭
- #성적관리프로그램
- html multimedia
- #CallByAddress
- #C++ 연산자함수오버로딩
- #JAVASCRIPT
- html video
- html id
- #2차원배열
- hyperledger transaction
- mac terminal command
- docker example
- #1차원배열
- #자바상속#자바이즈어#is~a
- git flow
- 토큰경제
- #3차원배열
- html youtube
- #bubbleSort
- html object
- #binary
- html code
Archives
- Today
- Total
A sentimental robot
상속 본문
상속
- 코드의 재활용, 유지보수
- 부모클래스 : 자식의 공통된 사항을 가지고 있다.
자식클래스 : 자식만의 고유한 기능을 추가만 하면 된다.
- 상속방식
1) private 상속 : has~a >> 잘 안쓰임
부모의 멤버가 모두 자식으로 상속되었을 때 private으로 바뀐다.
2) protected 상속 : has~a >> 잘 안쓰임
부모의 public 멤버가 protected로 바뀌어서 상속
3) public 상속 : is~a
있는 그대로 상속이 된다.
ex1) 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 |
#include<iostream>
#include<cstring>
#pragma warning(disable:4996)
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;
}
void setc1(int n) {
c1 = n;
}
int geta1()const {
return a1;
}
int getb1()const {
return b1;
}
int getc1()const {
return c1;
}
};
class B : private A {
int a2; // 4
protected:
int b2; // 5
public:
int c2; // 6
void seta2(int n) {
a2 = n;
}
void setb2(int n) {
b2 = n;
}
void setc2(int n) {
c2 = n;
}
int geta2()const {
return a2;
}
int getb2()const {
return b2;
}
int getc2()const {
return c2;
}
//class A (a1,b1,c1)메소드 접근
void seta1(int n) {
A::seta1(n);
}
void setb1(int n) {
A::setb1(n);
}
void setc1(int n) {
A::setc1(n);
}
int geta1()const {
return A::geta1();
}
int getb1()const {
return A::getb1();
}
int getc1()const {
return A::getc1();
}
};
class C :private B {
int a3; // 7
protected:
int b3; //8
public:
int c3; // 9
void seta3(int n) {
a3 = n; // class C private int a3
}
void setb3(int n) {
b3 = n; // class C protected int b3
}
int geta3()const {
return a3;
}
int getb3()const {
return b3;
}
// class B (a2,b2,c2) 메소드 접근
void seta2(int n) {
B::seta2(n);
}
void setb2(int n) {
B::setb2(n);
}
void setc2(int n) {
B::setc2(n);
}
int geta2()const {
return B::geta2();
}
int getb2()const {
return B::getb2();
}
int getc2()const {
return B::getc2();
}
// class B 메소드(private A (a1,b1,c1)접근하기 위해) 접근
void seta1(int n) {
B::seta1(n);
}
void setb1(int n) {
B::setb1(n);
}
void setc1(int n) {
B::setc1(n);
}
int geta1()const {
return B::geta1();
}
int getb1()const {
return B::getb1();
}
int getc1()const {
return B::getc1();
}
};
void main() {
C cc;
cc.seta1(1);
cc.setb1(2);
cc.setc1(3);
cc.seta2(4);
cc.setb2(5);
cc.setc2(6);
cc.seta3(7);
cc.setb3(8);
cc.c3 = 9;
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 |
ex2) protected 상속
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 |
#include<iostream>
#include<cstring>
#pragma warning(disable:4996)
using namespace std;
class A {
int a1; // 1
protected:
int b1; //2
public:
int c1; // 3
void seta1(int n) {
a1 = n;
}
int geta1()const {
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()const {
return a2;
}
};
class C :protected B {
int a3; // 7
protected:
int b3; //8
public:
int c3; // 9
void seta3(int n) {
a3 = n;
}
void setb3(int n) {
b3 = n;
}
// protected B (b2,c2)
void setb2(int n) {
b2 = n;
}
void setc2(int n) {
c2 = n;
}
// protected A (b1,c1)
void setb1(int n) {
b1 = n;
}
void setc1(int n) {
c1 = n;
}
// getter
int geta3()const {
return a3;
}
int getb3()const {
return b3;
}
int getb2()const {
return b2;
}
int getc2()const {
return c2;
}
int getb1()const {
return b1;
}
int getc1()const {
return c1;
}
//private A (a1)
void seta1(int n) {
A::seta1(n);
}
// private B(a2)
void seta2(int n) {
B::seta2(n);
}
int geta1()const {
return A::geta1();
}
int geta2()const {
return B::geta2();
}
};
void main() {
C cc;
cc.seta1(1);
cc.setb1(2);
cc.setc1(3);
cc.seta2(4);
cc.setb2(5);
cc.setc2(6);
cc.seta3(7);
cc.setb3(8);
cc.c3 = 9;
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 |
ex3) public 상속
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 |
#include<iostream>
#include<cstring>
#pragma warning(disable:4996)
using namespace std;
class A {
int a1; // 1
protected:
int b1; //2
public:
int c1; // 3
void seta1(int n) {
a1 = n;
}
int geta1()const {
return a1;
}
};
class B : public A {
int a2; // 4
protected:
int b2; //5
public:
int c2; // 6
void seta2(int n) {
a2 = n;
}
int geta2()const {
return a2;
}
// private int a1
void seta1(int n) {
A::seta1(n);
}
int geta1()const {
return A::geta1();
}
};
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;
}
void setb1(int n) {
b1 = n;
}
void setb2(int n) {
b2 = n;
}
int geta3()const {
return a3;
}
int getb3()const {
return b3;
}
int getb1()const {
return b1;
}
int getb2()const {
return b2;
}
};
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() << 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;
} |
cs |
'C++ ' 카테고리의 다른 글
순수 가상함수 (0) | 2018.10.02 |
---|---|
virtual function (0) | 2018.10.01 |
가상상속 (0) | 2018.10.01 |
연산자함수 오버로딩 예제 (0) | 2018.09.21 |
iostream operator function overloading (0) | 2018.09.20 |