1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071 |
- //类做友元
- #if(0)
- #include <iostream>
- class Building;
- class GoodGay
- {
- public:
- GoodGay();
- void visit(); //参观函数 访问Building中的属性
-
- Building* building;
- };
- class Building
- {
- //GoodGay这个类是本类的好朋友
- friend class GoodGay;
- public:
- Building();
- public:
- std::string m_SittingRoom; //客厅
- private:
- std::string m_BedRoom; //卧室
- };
- //类外写成员函数:
- Building::Building()
- {
- m_SittingRoom = "客厅";
- m_BedRoom = "卧室";
- }
- GoodGay::GoodGay()
- {
- //创建建筑物对象
- building = new Building;
- }
- void GoodGay::visit()
- {
- std::cout << "好基友类正在访问:" << building->m_SittingRoom << std::endl;
- std::cout << "好基友类正在访问:" << building->m_BedRoom << std::endl;
- }
- void test01()
- {
- GoodGay gg;
- gg.visit();
- }
- int main()
- {
- test01();
- system("pause");
- return 0;
- }
- #endif
|