自考新教材-p181
源程序:
#include<iostream>
using namespace std;
class another;
class Base
{
private:
float x;
public:
void print(const another &K);
};
class Derived:public Base
{
private:
float y;
};
class another
{
private:
int aaa;
public:
another()
{
aaa=100;
}
friend void Base::print(const another &K); //基类的成员函数声明为本类的友元
};
void Base::print(const another &K)
{
cout<<"Base:"<<K.aaa<<endl; //可以访问私有成员变量
}
int main()
{
Base a;
Derived d;
another ano;
a.print(ano);
d.print(ano);
return 0;
}
运行结果: