#include <iostream>
#include <string>
using namespace std;
class Building {
//告诉编译器 goodGay类中的visit成员函数 是Building好朋友,可以访问私有内容
friend void goodGay::visit();
public:
//构造函数初始化:
Building() {
m_SittingRoom = "客厅";
m_BedRoom = "卧室";
}
//属性
public:
string m_SittingRoom;
private:
string m_BedRoom;
};
class goodGay {
public:
//构造函数初始化
goodGay() {
building = new Building;
}
public:
void visit() {//我们让visit()函数比较特殊,使它能够访问building类中私有属性
cout << "visit()函数正在访问:" << building->m_SittingRoom << endl;
cout << "visit()函数正在访问:" << building->m_BedRoom << endl;
}
void visit2() {//我们让visit2()函数只能够访问building类中公有属性
cout << "visit2()函数正在访问:" << building->m_SittingRoom << endl;
cout << "visit2()函数正在访问:" << building->m_BedRoom << endl;
}
private:
//属性 是一个building类创建的指针,供goodgay访问
Building* building;
};
void test01() {
goodGay gg;
gg.visit();
gg.visit2();
}
int main() {
test01();
system("pause");
return 0;
} |
|