类和对象-友元-友元类

(1)这个代码的逻辑是在一个类中创建另一个类的指针,通过new动态内存分配来将,building指向Building类的一个对象

【1】这个代码将函数定义在类外

using namespace std;
class Building;
class GoodGay{
	public:
		GoodGay();
		void visit();
		Building *building;
};
class Building{
	friend class GoodGay;
	public:
		Building();
	public:
		string m_SittingRoom;
	private:
		string m_BedRoom;
};
Building::Building(){
	m_SittingRoom="客厅";
	m_BedRoom="卧室";
}
GoodGay::GoodGay(){
	building= new Building;
}
void GoodGay::visit(){
	cout<<"好基友类正在访问: "<<building->m_BedRoom<<endl;
}
void test01(){
	GoodGay gg;
	gg.visit();
}
int main()
{
	test01();
}

代码的关键点:friend class GoodGay;将类转换为友元类

全部评论

相关推荐

炬火初现:为什么会没有面试啊,我有些学长双非cpp都只有小厂实习,最后都还是能面不少的啊,也有进腾讯云智啥的。我个人感觉可能简历太杂了,可以压缩精炼一下。
点赞 评论 收藏
分享
评论
1
收藏
分享

创作者周榜

更多
牛客网
牛客企业服务