#include<stdio.h>
#include<iostream>
#include<string>
using namespace std;
class Building {
// GoodGay类是本类的好朋友,可以访问本类中私有的成员
friend class GoodGay;
public:
Building();
public:
string m_SittingRoom; // 客厅
private:
string m_BedRoom; // 卧室
};
// 类做友元
class GoodGay {
public:
GoodGay();
void visit(); // 参观函数 访问Building中的属性
Building* building;
};
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();
system("pause");
return 0;
}
友元-类做友元
最新推荐文章于 2024-08-23 11:29:58 发布