\
1.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
|
#include "stdafx.h"
#include<iostream>;
using namespace std;
//private 상속관계(잘안쓴다)
//C->B->A 하나 하나 올라와야한다
class A {
int a1;//1 화면에 나오고 싶어
protected:
int b1;//2
public:
int c1;//3
//setter,getter
void setA1(int n) { this->a1 = n; }
int getA1() const { return this->a1; }
void setB1(int n) { this->b1 = n; }
int getB1() const { return this->b1; }
void setC1(int n) { this->c1 = n; }
int getC1() const { return this->c1; }
};
class B :protected A {
int a2; //4
protected:
int b2; //5
public:
int c2; //6
//setter,getter
void setA2(int n) { this->a2 = n; }
int getA2() const { return this->a2; }
void setB2(int n) { this->b2 = n; }
int getB2() const { return this->b2; }
void setC2(int n) { this->c2 = n; }
int getC2() const { return this->c2; }
// 직접 접근 불가능 해서 함수로 표현
void setA1(int n) { A::setA1(n); }
int getA1() const { return A::getA1(); }
void setB1(int n) { A::setB1(n); }
int getB1() const { return A::getB1(); }
void setC1(int n) { A::setC1(n); }
int getC1() const { return A::getC1(); }
};
class C : protected B {
int a3;//7
protected:
int b3;//8
public:
int c3;//9
//A->B->C
void setA1(int n) { B::setA1(n); }//B 클래스꺼를 땡겨온당
int getA1()const { return B::getA1(); }
void setB1(int n) { B::setB1(n); }
int getB1()const { return B::getB1(); }
void setC1(int n) { B::setC1(n); }
int getC1()const { return B::getC1(); }
//B->C
void setA2(int n) { B::setA2(n); }// B 클래스꺼를 땡겨온당
int getA2()const { return B::getA2(); }
void setB2(int n) { B::setB2(n); }
int getB2()const { return B::getB2(); }
void setC2(int n) { B::setC2(n); }
int getC2()const { return B::getC2(); }
void setA3(int n) { this->a3 = n; } // 직접 접근
int getA3() const { return this->a3; }
void setB3(int n) { this->b3 = n; }
int getB3() const { return this->b3; }
};
void main() {
// 직접 접근 가능한 건 직접 값을 표현할 것
/*
a1 = 1;
b1 = 2;
c1 = 3;
a2 = 4;
b2 = 5;
c2 = 6;
a3 = 7;
b3 = 8;
c3 = 9;
*/
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;//main에서 바로 접근
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 |
2. protexted 상속관계
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
|
#include "stdafx.h"
#include<iostream>;
using namespace std;
//protexted 상속관계
class A {
int a1;//1 화면에 나오고 싶어
protected:
int b1;//2
public:
int c1;//3
//setter,getter
void setA1(int n) { this->a1 = n; }
int getA1() const { return this->a1; }
void setB1(int n) { this->b1 = n; }
int getB1() const { return this->b1; }
void setC1(int n) { this->c1 = n; }
int getC1() const { return this->c1; }
};
class B :protected A {
int a2; //4
protected:
int b2; //5
public:
int c2; //6
//setter,getter
void setA2(int n) { this->a2 = n; }
int getA2() const { return this->a2; }
void setB2(int n) { this->b2 = n; }
int getB2() const { return this->b2; }
void setC2(int n) { this->c2 = n; }
int getC2() const { return this->c2; }
};
class C : protected B {
int a3;//7
protected:
int b3;//8
public:
int c3;//9
// 직접 접근 불가능한 건 '함수'로 표현
//A->C // A 클래스꺼를 땡겨온당
void setA1(int n) { A::setA1(n); } // 오버라이딩함수 부모와 자식이 같아야한다.//:: = 우선시
int getA1()const { return A::getA1(); }
void setB1(int n) { A::setB1(n); }
int getB1()const { return A::getB1(); }
void setC1(int n) { A::setC1(n); }
int getC1()const { return A::getC1(); }
//B->C
void setA2(int n) { B::setA2(n); }// B 클래스꺼를 땡겨온당
int getA2()const { return B::getA2(); }
void setB2(int n) { B::setB2(n); }
int getB2()const { return B::getB2(); }
void setC2(int n) { B::setC2(n); }
int getC2()const { return B::getC2(); }
void setA3(int n) { this->a3 = n; } // 직접 접근
int getA3() const { return this->a3; }
void setB3(int n) { this->b3 = n; }
int getB3() const { return this->b3; }
};
void main() {
// 직접 접근 가능한 건 직접 값을 표현할 것
/*
a1 = 1;
b1 = 2;
c1 = 3;
a2 = 4;
b2 = 5;
c2 = 6;
a3 = 7;
b3 = 8;
c3 = 9;
*/
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;//main에서 바로 접근
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 |
3.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
|
#include "stdafx.h"
#include<iostream>;
using namespace std;
//protexted 상속관계
class A {
int a1;//1 화면에 나오고 싶어
protected:
int b1;//2
public:
int c1;//3
//setter,getter
void setA1(int n) { this->a1 = n; }
int getA1() const { return this->a1; }
void setB1(int n) { this->b1 = n; }
int getB1() const { return this->b1; }
void setC1(int n) { this->c1 = n; }
int getC1() const { return this->c1; }
};
class B :protected A {
int a2; //4
protected:
int b2; //5
public:
int c2; //6
//setter,getter
void setA2(int n) { this->a2 = n; }
int getA2() const { return this->a2; }
void setB2(int n) { this->b2 = n; }
int getB2() const { return this->b2; }
void setC2(int n) { this->c2 = n; }
int getC2() const { return this->c2; }
};
class C : protected B {
int a3;//7
protected:
int b3;//8
public:
int c3;//9
// 직접 접근 불가능한 건 '함수'로 표현
//A->C // A 클래스꺼를 땡겨온당
void setA1(int n) { A::setA1(n); } // 오버라이딩함수 부모와 자식이 같아야한다.//:: = 우선시
int getA1()const { return A::getA1(); }
void setB1(int n) { A::setB1(n); }
int getB1()const { return A::getB1(); }
void setC1(int n) { A::setC1(n); }
int getC1()const { return A::getC1(); }
//B->C
void setA2(int n) { B::setA2(n); }// B 클래스꺼를 땡겨온당
int getA2()const { return B::getA2(); }
void setB2(int n) { B::setB2(n); }
int getB2()const { return B::getB2(); }
void setC2(int n) { B::setC2(n); }
int getC2()const { return B::getC2(); }
void setA3(int n) { this->a3 = n; } // 직접 접근
int getA3() const { return this->a3; }
void setB3(int n) { this->b3 = n; }
int getB3() const { return this->b3; }
};
void main() {
// 직접 접근 가능한 건 직접 값을 표현할 것
/*
a1 = 1;
b1 = 2;
c1 = 3;
a2 = 4;
b2 = 5;
c2 = 6;
a3 = 7;
b3 = 8;
c3 = 9;
*/
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;//main에서 바로 접근
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 |
<1,2,3 출력값>
저의 블로그 봐주셔서 감사합니다
재.미.있.게 보셧다면 아래 하트 ❤(공감) 과 댓글 부탁 드려요 .
템플릿(template) (0) | 2018.03.16 |
---|---|
[c++]동적 메모리 이용 Stack/Queue 예제 (0) | 2018.03.14 |
[C++]암시적 오버로딩(overloading) 정의 / 코드 (0) | 2018.03.11 |
[C++]명시적 오버로딩(Overloading) 정의 조건 (0) | 2018.03.11 |
[C++]레퍼러스 (call by reference) (0) | 2018.03.11 |