友元函数可以访问类的对象的私有成员变量,成员函数,代码如下:
class TestClass
{
private:
int length;
int width;
public:
TestClass(int len,int wide){ length=len; width=wide;};
friend int CaculateArea(TestClass test); //注意要加关键字friend
friend int CaculateCircle(TestClass test);
};
int CaculateArea(TestClass test) //友元函数可以访问test对象的私有成员变量length,width
{
return test.length * test.width;
}
int CaculateCircle(TestClass test)
{
return 2*(test.length + test.width);
}
int main()
{
TestClass testClass(2,3);
cout<
}