반응형
1. 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 | #include <iostream> using namespace std; 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; } }; class B : public 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; } }; class C : public B { int a3; // 7 protected: int b3; // 8 public: int c3; // 9 // setter, getter 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.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 \ |
2. 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 | #include <iostream> using namespace std; 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 // setter, getter 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(); } void setA2(int n) { B::setA2(n); } 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); // ? 왜 c1, c2는 직접 접근이 가능한가 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 |
3. 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 | #include <iostream> using namespace std; 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 : private A { int a2; // 4 protected: int b2; // 5 public: int c2; // 6 // setter, getter 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(); } 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 : private B { int a3; // 7 protected: int b3; // 8 public: int c3; // 9 // setter, getter void setA1(int n) { B::setA1(n); } 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(); } void setA2(int n) { B::setA2(n); } 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); // ? 왜 c1, c2는 직접 접근이 가능한가 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 |
반응형