友元函数的声明(P129)
/*
友元的概念破坏了类的封装性和信息隐蔽性,但有助于数据共享,能够提高程序执行的效率。
友元机制是对一些类外的函数打开一个特殊通道,授权它们能够访问本类的私有成员变量。
friend 返回值类型 函数名(参数表);
*/
#include<iostream>
#include<cmath>
using namespace std;
class Pixel; //前向引用声明
class Test
{
public:
void printX(Pixel p);
};
class Pixel
{
private:
int x,y;
public:
Pixel(int x0,int y0)
{
x = x0;
y = y0;
}
void printxy()
{
cout<<"pixel:("<<x<<","<<y<<")"<<endl;
}
friend double getDist(Pixel p1,Pixel p2); //友元函数,全局函数
friend void Test::printX(Pixel p); //类Test的成员函数为本类的友元函数
};
void Test::printX(Pixel p)
{
cout<<"x="<<p.x<<"\ty="<<p.y<<endl;
return;
}
double getDist(Pixel p1,Pixel p2) //友元函数在类体外定义
{
double xd = double(p1.x - p2.x);
double yd = double(p1.y - p2.y);
return sqrt(xd*xd + yd*yd);
}
int main()
{
Pixel p1(0,0),p2(10,10);
p1.printxy();
p2.printxy();
cout<<"(p1,p2)间距离 ="<<getDist(p1,p2)<<endl;
Test t;
cout<<"从友元函数中输出--"<<endl;
t.printX(p1); //通过对象调用类的成员函数
t.printX(p2);
return 0;
}

浙公网安备 33010602011771号