protected멤버

구대호·2021년 11월 19일

<private 상속 접근제어>

  • private 상속 접근인 경우에는 기본 클래스의 모든 멤버가 파생 클래스의 private 멤버가 된다.
  • 기본 클래스의 public 멤버들은 클래스의 외부 (main함수 등)에서는 접근이 불가능 하고, 오직 파생클래스의 멤버함수에 의해서만 접근할 수 있다.
  • 다른 사람 모르게 자기 자식에게만 비밀리에 상속함

<private 상속 접근제어로 상속 1>

#include <iostream>
using std::cout;
using std::endl;
class A
{
	int x;
public:
	void setX(int i) { x = i; }
	void showX() { cout << x << endl; }
};
class B :private A //비공개적으로 상속
{
	int y;
public:
	void setY(int i) { y = i; }
	void showY() { cout << y << endl; }
};
int main()
{
	A aa;
	B bb;
	aa.setX(1);
	aa.showX();
	bb.setY(2); // 파생클래스의 멤버접근
	bb.showY(); // 파생클래스의 멤버접근
	return 0;
}
<파생 클래스 B는 이렇게 보여야 해요!>
class B :private A {
	int y;
	void setX(int i) { x = i; }
	void showX() { cout << x << endl; }
public:
	void setY(int i) { y = i; }
	void showY() { cout << y << endl; }
};

In-class member initializers

#include <iostream>
using std::cout;
using std::endl;
class A
{
int x = 1 ;	// In-class member initializers
public:
void setX(int i) { x = i; }
int getX() { return x ; }
};
int main()
{
A a1; //디폴트 생성자 호출, 눈에 안보이는 A(){}
cout << a1.getX()<<endl;
return 0;
}
#include <iostream>
using std::cout;
using std::endl;
class A
{
int x = 1 ;
public:
A() { x = 2; } //A():x(2){}
void setX(int i) { x = i; }
int getX() { return x ; }
};
int main()
{
A a1; //디폴트 생성자는 사라짐
cout << a1.getX()<<endl;
return 0;
}

<private 상속 접근제어의 용도>

#include <iostream>
using std::cout;
using std::endl;
class A
{
	int x;
public:
	void setX(int i) { x = i; }
	void showX() { cout << x << endl; }
};
class B :private A //비공개적으로 상속받는다
{
	int y;
public:
	void setXY(int i, int j) { setX(i); y = j; }
	// 기본 클래스의 public 멤버 접근
	void showXY() { showX(); cout << y << endl; }
};
int main()
{
	B bb;
	bb.setXY(1, 2); // 파생클래스의 멤버접근
	bb.showXY(); 	// 파생클래스의 멤버접근
	return 0;
}
<파생 클래스 B는 이렇게 보여야 해요!>
class B :private A {
	int y;
	void setX(int i) { x = i; }
	void showX() { cout << x << endl; }
public:
	void setXY(int i, int j) { setX(i); y = j; }
	void showXY() { showX(); cout << y << endl; }
};

<protected 상속 접근제어 속성>

  • 거의 사용하지 않음
  • 어떠한 방식으로 상속 받더라도 파생 클래스는 기본 클래스의 private 멤버를 접근할 수 없다.
  • 파생 클래스가 기본 클래스의 어떤 멤버를 접근할 수 있으려면 그 멤버는 public이 되어야 한다.
  • 그러나 기본 클래스에서 private 멤버 속성을 유지하면서 파생 클래스가 접근하는 것이 가능하도록 하는 경우가 있는데, 이 때 사용하는 것이 protected 접근이다.
  • protected 접근은 기본 클래스의 protected 멤버들이 파생 클래스의 멤버에 의해 접근될 수
    있다는 것을 제외하고는 private 접근과 같다.
  • 기본 클래스나 파생클래스 이외의 부분에서는 protected 멤버를 접근할 수 없다.
<a,b가 자식에게 상속됨>
class A
{
protected:
	int a, b;
public:
	……
};
<a,b가 자식에게 상속 안됨>
class A
{
private:
	int a, b;
public:
	……
};

<protected 멤버변수>

#include <iostream>
using std::cout;
using std::endl;
class A
{
protected: //private이라면?
	int a, b;
public:
	void setAB(int i, int j) { a = i; b = j; }
};
class B :public A
{
	int c; // private
public:
	void setC(int n) { c = n; }
	void showABC() { cout << a << b << c << endl; }
	//기본 클래스의 protected 멤버들은
	//파생 클래스의 멤버에 의해 접근될 수 있다.
};
int main()
{
	A aa;
	B bb;
	bb.setAB(1, 2);
	bb.setC(3);
	bb.showABC();
	return 0;
}

<protected멤버 + public상속>

  • 클래스를 만들 때 클래스 외부에서는 접근하지 못하게 하고 자식클래스에서는 마음대로 접근할 수 있게 하려면 멤버의 속성을 protected로 사용하라!
  • 상속은 public으로 하라!
profile
개발 초보 대학생

0개의 댓글