[C++] 상속관련

정민리·2022년 12월 1일
0
class Base {

	int a;

protected:
	void setA(int a) {
		this->a = a;

	}

	void showA() {
		cout << a << endl;
	}

};

class Derived :private Base {

	int b;

protected:
	void setB(int b) {
		this->b = b;
	}

public:
	void showB() {
		setA(5); //얘네는 여전히 접근이 됨  (protected이기 때문)
		showA(); //얘도 protected이기 때문에 접근가능
		cout << b << endl;
	}
};

class GrandDerived :private Derived {
	int c;
protected:
	void setAB(int x) {
		//setA(x); // 컴파일 오류 -> 상속이 private으로 되면 접근이 안된다  -> setA()와 showA()는 private인 상태에서 이미 GrandDerived에 온거고
		//showA();
		//여긴 GrandDerived에 오면서 private이 됨
		setB(x);// Derived에서 접근제어자가 protected이기 때문에 접근 가능 
		showB();// Derived에서 접근제어자가 protected이기 때문에 접근 가능 
	}
};
profile
우당탕탕 빙글빙글💦

0개의 댓글