#ifndef BOY_H
#define BOY_H
#include <iostream>
#include <string>
#include "Girl.h"
using namespace std;
class Boy
{
public:
Boy(){}
Boy(string name, string phone, string face);
string m_strFace;
//若將一個(gè)類申明為友元,
//則在該類所有函數(shù)中都可以訪問Boy的私有數(shù)據(jù)
friend class Girl;
//若將一個(gè)類中的某個(gè)成員函數(shù)申明為友元
//則只能在該函數(shù)中訪問Boy的私有數(shù)據(jù)
//friend void Girl::getBoyName(Boy &boy);
//友元函數(shù)
//在友元函數(shù)可以通過對(duì)象直接訪問和操作該對(duì)象的私有數(shù)據(jù)
//友元破壞了類的封裝性,盡量不要用
friend void fun(Boy &boy);
private:
string m_strName;
string m_strPhone;
};
void fun(Boy &boy);
#endif
#ifndef GIRL_H
#define GIRL_H
#include <iostream>
#include <string>
using namespace std;
class Boy;
class Girl
{
public:
void getBoyName(Boy &boy);
void getBoyPhone(Boy &boy);
};
#endif
#include "Girl.h"
#include "boy.h"
void Girl::getBoyName(Boy &boy)
{
cout << boy.m_strName << endl;
}
void Girl::getBoyPhone(Boy &boy)
{
cout << boy.m_strPhone << endl;
}
最后編輯于 :
?著作權(quán)歸作者所有,轉(zhuǎn)載或內(nèi)容合作請(qǐng)聯(lián)系作者
【社區(qū)內(nèi)容提示】社區(qū)部分內(nèi)容疑似由AI輔助生成,瀏覽時(shí)請(qǐng)結(jié)合常識(shí)與多方信息審慎甄別。
平臺(tái)聲明:文章內(nèi)容(如有圖片或視頻亦包括在內(nèi))由作者上傳并發(fā)布,文章內(nèi)容僅代表作者本人觀點(diǎn),簡(jiǎn)書系信息發(fā)布平臺(tái),僅提供信息存儲(chǔ)服務(wù)。