code/C++

[c++] private 상속

shallot 2017. 4. 20. 14:15

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
#include<iostream>
#include<string>
#pragma warning(disable:4996)
using namespace std;
 
class A {
    int a1;
protected:
    int b1;
public:
    int c1;
    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 : private A {
    int a2; ;//a1,b1,c1//b1,c1접근 가능
protected:
    int b2;
public:
    int c2;
    void setA1B(int a1) {
        setA1(a1);
    }
    int getA1B()const
    {
        return getA1();
    }
    void setB1B(int b1) {
        setB1(b1);
    }
    int getB1B()const
    {
        return getB1();
    }
    void setC1B(int c1) {
        setC1(c1);
    }
    int getC1B()const
    {
        return getC1();
    }
    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 : private B { //제일 최하위 class
    int a3; //a1,a2,b1,b2,c1,c2 //b2,c2접근 가능
 
protected:
    int b3;
public:
    int c3;
    void setA1C(int a1) {
        setA1B(a1);
    }
    int getA1C()const
    {
        return getA1B();
    }
    void setB1C(int b1) {
        setB1B(b1);
    }
    int getB1C()const
    {
        return getB1B();
    }
    void setC1C(int c1) {
        setC1B(c1);
    }
    int getC1C()const
    {
        return getC1B();
    }
    void setA2C(int a2) {
        setA2(a2);
    }
    int getA2C()const
    {
        return getA2();
    }
    void setB2C(int b2) {
        this->b2 = b2;
    }
    int getB2C()const
    {
        return getB2();
    }
    void setC2C(int c2) {
        this->c2 = c2;
    }
    int getC2C()const
    {
        return getC2();
    }
    void setA3(int a3) {
        this->a3 = a3;
    }
    int getA3()const
    {
        return a3;
    }
    void setB3(int b3) {
        this->b3 = b3;
    }
    int getB3()const
    {
        return b3;
    }
    void setC3(int c3) {
        this->c3 = c3;
    }
    int getC3()const
    {
        return c3;
    }
};
 
void main() {
    C cc; //c1,c2,c3는 접근이 가능
    cc.setA1C(1);
    cc.setB1C(2);
    cc.setC1C(3);
    cc.setA2C(4);
    cc.setB2C(5);
    cc.setC2C(6);
    cc.setA3(7);
    cc.setB3(8);
    cc.setC3(9);
 
    cout << cc.getA1C() << "\t" << cc.getB1C() << "\t" << cc.getC1C() << "\t" << cc.getA2C() << "\t"
        << cc.getB2C() << "\t" << cc.getC2C() << "\t" << cc.getA3() << "\t" << cc.getB3() << "\t" << cc.getC3() << endl;
}