1. 程式人生 > >C++----整個類做友元類

C++----整個類做友元類

//類做友元類:
class Building
{
	friend class goodGay;
public:
	Building();
public:
	string m_sittingroom;	//客廳
private:
	string m_bedroom;		//臥室
};
Building::Building()
{
	this->m_bedroom = "臥室";
	this->m_sittingroom = "客廳";
}
class goodGay
{
public:
	goodGay();
	
	void visit();

private:
	Building * building;
};
//類內宣告,類外實現;
goodGay::goodGay()
{
	building = new Building;
}
void goodGay::visit()
{
	cout<<"基友正在"<<this->building->m_sittingroom<<endl;
	//要訪問私有屬性的元素;
	//在Building 類中定義友元類:friend class goodGay;
	cout << "基友正在" << this->building->m_bedroom << endl;

}
void test01()
{
	goodGay g1;
	g1.visit();
}
int main()
{
	test01();
	return 0;
}