friend
friend
friend 클래스
#include <iostream>
#include <cstring>
using namespace std;
class Boy; // Boy 클래스가 있다고 알림
class Girl {
char name[20];
char phone[20];
public:
Girl(const char *name, const char *phone) {
strcpy(this->name, name);
strcpy(this->phone, phone);
}
friend class Boy; // Boy 클래스를 친구로 선언
};
class Boy {
char name[20];
public:
Boy(const char*name) {
strcpy(this->name, name);
}
void showInfo(Girl &frn) {
cout << "나는 " << name << "입니다." << endl;
cout << "내 여자친구 이름은 " << frn.name << "입니다." << endl;
cout << "그녀의 전화번호는 " << frn.phone << "입니다" << endl;
}
};
int main() {
Boy boy("홍길동");
Girl girl("김지은", "010-0000-1234");
boy.showInfo(girl);
return 0;
}friend 함수
Last updated