friend
2022. 4. 10. 12:17ㆍC++
private 로 선언된 멤버변수와 멤버함수는 클래스 내에서만 접근가능하다.
class Boy
{
private:
int height;
friend class Girl; //Girl 클래스를 friend로 선언함
public:
Boy(int len) : height(len)
{}
};
class Girl
{
private:
char phNum[20];//전화번호
public:
Girl(char* num)
{
strcpy(phNum, num);
}
void ShowYoutFriendInfo(Boy &frn)
{
cout<<"His height: "<<frn.height<<endl;//private 멤버에 접근
}
};
friend 선언은 객체지향의 정보은닉을 무너뜨리는 문법이기 때문에 가급적 사용하지 않는 것을 추천한다.