본문 바로가기

c++

c++ public protected private

 

public

 

 

#include <iostream>
using namespace std;
 
/*
 상속 ==> 코드의 재활용 유지보수
 
 부모클래스: 자식의 공통된 사황을 가지고 있다
 자식클래스: 자신만의 고유한 기능을 추가만 하면된다.
 1.상속방식
 1> private 상속 : has ~ a;
     부모의 멤버가 모두 자식으로 상속했을 때 : private로 변한다. (내부적으로 변한다.)
 2> protected 상속: has ~ a;
     부모의 public 멤버가 protected로 변환하여 상속된다.
 3> public 상속 : is ~a;
     있는 그대로 상속이 된다.
*/
 
class A {
 
private
    int a1 =1;
 
protected:
    int b1 =2;
 
public:
    int c1 =3 ;
 
    int getA1() {
        return a1;
    }
    int getB1() {
        return b1;
    }
    int gerC1() {
        return c1;
    }
 
};
 
class B : public A {
 
private:
    int a2 =4// a1,b1,c1
 
protected:
    int b2 =5;
 
public:
    int c2 =6;
 
 
    int getA2() {
        return a2;
    }
    int getB2() {
        return b2;
    }
    int getC2() {
        return c2;
    }
 
 
};
 
class C : public B{
 
 
private:
    int a3=7;
 
protected:
    int b3=8;
 
public:
    int c3=9;
 
    // A클래스 접근
    void setA1() {
        cout << "a1: " << A::getA1() << endl;
    }
    void setB1() {
        cout << "b1: " << A::getB1() << endl;
    }
    void setC1() {
        cout << "c1: " << A::gerC1() << endl;
    }
    // B 클래스 접근
    void setA2() {
        cout << "a2: " << B::getA2() << endl;
    }
    void setB2() {
        cout << "b2: " << B::getB2() << endl;
    }
    void setC2() {
        cout << "c2: " << B::getC2() << endl;
    }
 
    int getA3() {
        return a3;
    }
    int getB3() {
        return b3;
    }
    int getC3() {
        return c3;
    }
    
 
};
int main() {
 
    C cc;
    cout << "c3 :" << cc.getC3() << endl;
    cout << "b3 :" << cc.getB3() << endl;
    cout << "a3 :" << cc.getA3() << endl;
    cc.setA2();
    cc.setB2();
    cc.setC2();
    cc.setA1();
    cc.setB2();
    cc.setC1();
    
    return 0;
}

 

protected

 

#include <iostream>
using namespace std;
 
/*
 상속 ==> 코드의 재활용 유지보수
 
 부모클래스: 자식의 공통된 사황을 가지고 있다
 자식클래스: 자신만의 고유한 기능을 추가만 하면된다.
 1.상속방식
 1> private 상속 : has ~ a;
     부모의 멤버가 모두 자식으로 상속했을 때 : private로 변한다. (내부적으로 변한다.)
 2> protected 상속: has ~ a;
     부모의 public 멤버가 protected로 변환하여 상속된다.
 3> public 상속 : is ~a;
     있는 그대로 상속이 된다.
*/
 
class A {
 
private
    int a1 =1;
 
protected:
    int b1 =2;
 
public:
    int c1 =3 ;
 
    int getA1() {
        return a1;
    }
 
};
 
class B : protected A {
 
private:
    int a2 =4// a1,b1,c1
 
protected:
    int b2 =5;
 
public:
    int c2 =6;
 
 
    int getA2() {
        return a2;
    }
    int getB1() {
        return b1;
    }
    int getC1() {
        return c1;
    }
    void get_A1() {
        cout << "a1 :"<< getA1() << endl;
    }
 
};
 
class C : protected B{
 
 
private:
    int a3=7;
 
protected:
    int b3=8;
 
public:
    int c3=9;
 
    int getB3() {
        return b3;
    }
    int getA3() {
        return a3;
    }
    int getB2() {
        return b2;
    }
    int getC2() {
        return c2;
    }
    void get() {
        
        cout << "a2: " << getA2() << endl;
        cout << "b1: "<< getB1() << endl;
        cout << "c1: " << getC1() << endl;
        get_A1();
    }
 
};
int main() {
 
    C cc;
    cout << "c3 :" << cc.c3 << endl;
    cout << "a3 :" << cc.getA3() << endl;
    cout << "b3 :" << cc.getB3() << endl;
    cout << "b2 :" << cc.getB2() << endl;
    cout << "c2 :" << cc.getC2() << endl;
    cc.get();
 
    return 0;
}

 

private

 

#include <iostream>
using namespace std;
 
/*
 상속 ==> 코드의 재활용 유지보수
 
 부모클래스: 자식의 공통된 사황을 가지고 있다
 자식클래스: 자신만의 고유한 기능을 추가만 하면된다.
 1.상속방식
 1> private 상속 : has ~ a;
     부모의 멤버가 모두 자식으로 상속했을 때 : private로 변한다. (내부적으로 변한다.)
 2> protected 상속: has ~ a;
     부모의 public 멤버가 protected로 변환하여 상속된다.
 3> public 상속 : is ~a;
     있는 그대로 상속이 된다.
*/
 
class A {
 
private
    int a1 =1;
 
protected:
    int b1 =2;
 
public:
    int c1 =3 ;
 
    int getA1() {
        return a1;
    }
 
};
 
class B : protected A {
 
private:
    int a2 =4// a1,b1,c1
 
protected:
    int b2 =5;
 
public:
    int c2 =6;
 
 
    int getA2() {
        return a2;
    }
    int getB1() {
        return b1;
    }
    int getC1() {
        return c1;
    }
    void get_A1() {
        cout << "a1 :"<< getA1() << endl;
    }
 
};
 
class C : protected B{
 
 
private:
    int a3=7;
 
protected:
    int b3=8;
 
public:
    int c3=9;
 
    int getB3() {
        return b3;
    }
    int getA3() {
        return a3;
    }
    int getB2() {
        return b2;
    }
    int getC2() {
        return c2;
    }
    void get() {
        
        cout << "a2: " << getA2() << endl;
        cout << "b1: "<< getB1() << endl;
        cout << "c1: " << getC1() << endl;
        get_A1();
    }
 
};
int main() {
 
    C cc;
    cout << "c3 :" << cc.c3 << endl;
    cout << "a3 :" << cc.getA3() << endl;
    cout << "b3 :" << cc.getB3() << endl;
    cout << "b2 :" << cc.getB2() << endl;
    cout << "c2 :" << cc.getC2() << endl;
    cc.get();
 
    return 0;
}

 

 

'c++' 카테고리의 다른 글

reference 레퍼런스 계산기 구현  (0) 2018.12.31
c++ operator 연산자 구현  (0) 2018.12.31
c++ 동적 계산기 구현 ver1  (0) 2018.12.31
c++ string을 나만의 것으로 구현  (0) 2018.12.31