qfedu-cpp-level/day4/d6_2.cpp

44 lines
467 B
C++
Raw Permalink Normal View History

// 友元类
#include <iostream>
#include <cstring>
#include <cstdlib>
using namespace std;
class B;
class C
{
public:
void showB(B &b);
};
class B
{
friend class C;
private:
int x;
public:
B(int x) : x(x) {}
void show()
{
cout << "成员函数 x = " << x << endl;
}
};
void C::showB(B &b)
{
cout << "友元类 b.x = " << b.x << endl;
}
int main()
{
B b(100);
b.show();
C c;
c.showB(b);
return 0;
}