C++类做友元

类做友元

代码示例:
#include <iostream>
#include <string>
using namespace std;
//类做友元
class Building;
class GoodGay
{
   
public:
       GoodGay();
       void visit(); //参观函数 访问Building中的属性
       Building *building;
};
class Building
{
   
       //Goody类是本类的好朋友,可以访问本类的私有成员
       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_SittingRoom << endl;
       cout << "好基友类正在访问:" << building->m_BedRoom << endl;
}
void test01()
{
   
       GoodGay gg;
       gg.visit();
}
int main()
{
   
       test01();
       return 0;
}

欢迎关注微信公众号:黑马金牌编程

C/C++基础 文章被收录于专栏

本专栏收录C/C++编程语言相关知识

全部评论

相关推荐

码农索隆:想看offer细节
点赞 评论 收藏
分享
废物一个0offer:认真的吗二本本科找人工智能岗位
点赞 评论 收藏
分享
评论
点赞
收藏
分享

创作者周榜

更多
牛客网
牛客网在线编程
牛客网题解
牛客企业服务