#include < iostream >
using namespace std;
class parent
{
public :
parent(){value = 0;}
void methodA(){cout << "methodA call!" << endl;}
protected :
void methodB(){cout << "methodB call!" << endl;}
private :
int value;
};
class child1 : public parent
{
public :
child1(){value_child1 = 1;}
void methodC(){cout << "methodC call!" << endl; methodB();} //부모로부터 상속받았기때문에 methodB에 접근허가
protected :
void methodD(){cout << "methodD call!" << endl;}
private :
int value_child1;
};
class child2 : protected parent
{
public :
child2(){value_child2 = 2;}
void methodE(){cout << "methodE call!" << endl; methodB();} //부모로부터 상속받았기때문에 methodB에 접근허가
protected :
void methodF(){cout << "methodF call!" << endl; }
private :
int value_child2;
};
class child3 : private parent
{
public :
void methodG(){cout << "methodG call!" << endl; methodB();} //부모로부터 상속받았기때문에 methodB에 접근허가
};
void main()
{
child1 childtest;
childtest.methodA();
//childtest.methodB(); //에러발생 protected 멤버 접근 불가
childtest.methodC();
//childtest.methodD(); //에러발생 protected 멤버 접근 불가
//cout << childtest.value << endl; //에러발생 private 멤버 접근 불가
//cout << childtest.value_child1 << endl; //에러발생 private 멤버 접근 불가
cout << endl;
child2 childtest2;
//childtest2.methodA(); //에러발생 protected로 상속받았기때문에 public 이더라도 사용할 수 없음
//childtest2.methodB(); //에러발생 protected 멤버 접근 불가
childtest2.methodE();
//childtest2.methodF(); //에러발생 protected 멤버 접근 불가
//cout << childtest2.value << endl; //에러발생 private 멤버 접근 불가
cout << endl;
child3 childtest3;
childtest3.methodG();
}
실행결과
methodA call! methodE call! methodG call! |
출처 : 직접작성 및 Teach Yourself C++ - Visual Studio.NET 2003을 기준으로 작성됨
'Computer_language > C++' 카테고리의 다른 글
C++ class에서 함수정의에 사용된 static [출처] C++ class에서 함수정의에 사용된 static|작성자 정천사 (0) | 2009.01.12 |
---|---|
Static Class 에 대한 정의 (0) | 2009.01.12 |
정적 중첩 클래스 (0) | 2009.01.12 |
Ubuntu에서, c, c++관련 컴파일러 설치 [출처] Ubuntu에서, c, c++관련 컴파일러 설치|작성자 용이 (0) | 2009.01.12 |
나눈 값을 표현하는 방법 즉 double 크기의 표현 방법 (float 4비트 double 8비트) (0) | 2009.01.12 |