#include<iostream>
using namespace std;
class Building;
class Gooddey{
public:
friend Building;
Gooddey();
void visit();
Building *building;
};
class Building{
friend class Gooddey;
public:
Building(){
this->age = 10;
this->ac = 20;
}
public:
int age ;
private:
int ac ;
};
Gooddey::Gooddey(){
building = new Building;
}
void Gooddey::visit(){
cout << building->age << endl;
cout << building->ac << endl;
}
void test(){
Gooddey p;
p.visit();
}
int main(){
test();
}
类和类之间访问私有变量
最新推荐文章于 2024-10-04 22:43:57 发布
本文介绍了C++中类(class)Building和Gooddey的使用,展示了如何通过友元实现类之间的交互,以及如何在Gooddey类中创建并访问Building类的对象。test()函数用于测试对象的创建和访问方法。
摘要由CSDN通过智能技术生成