题目
Create three classes. The first class contains private data, and grants friendship to the entire second class and to a member function of the third class. In main( ), demonstrate that all of these work correctly.
代码
#include<iostream>
using namespace std;
class A;class B;
class C //友元函数
{
public:
void showC(A& aa);
};
class A
{
public:
friend void C::showC(A& aa);
A(int i=0){tmp = i;}
private:
int tmp;
friend class B;
};
void C::showC(A& aa){cout <<"from class C :" << aa.tmp<<endl;}
class B //友元类
{
public:
void showB(A& a)
{
cout << "from class B :" << a.tmp << endl;
}
};
int main()
{
int x;
cout << "type a number: ";//输入一个数字
cin >> x;
A testA(x);
B testB;
C testC;
testB.showB(testA);
testC.showC(testA);
return 0;
}