2012年(1008)
分类:
2012-08-01 11:09:00
原文地址:友元实例 作者:luozhiyong131
/*
* 普通函数作为友元函数
* Lzy 2011-8-4
*/
#include
using namespace std;
class Person
{
private:
int id;
public:
Person(int i):id(i){}
friend int Display(Person &p);
};
int Display(Person &p)
{
return p.id;
}
int main(void)
{
Person p(3);
cout<
return 0;
}
/*
* 成员函数作为友元函数
* Lzy 2011-8-4
*/
#include
using namespace std;
class ClassA;
class ClassB
{
public:
int Display(ClassA &a);
};
class ClassA
{
private:
int num;
public:
ClassA(int i):num(i){}
friend int ClassB::Display(ClassA &a);
};
int ClassB::Display(ClassA &a)
{return a.num;}
int main(void)
{
ClassA a(3);
ClassB b;
cout<
return 0;
}
/*
* 友元类
* Lzy 2011-8-4
*/
#include
#include
using namespace std;
class Point;
class Point
{
private:
float x, y;
public:
Point(const float i,const float j):x(i),y(j){}
friend class Distance;
};
class Distance
{
public:
float distance(Point &p1, Point &p2){return sqrt((p1.x-p2.x)*(p1.x-p2.x)+(p1.y-p2.y)*(p1.y-p2.y));}
};
int main(void)
{
Point p1(3.0,4.0), p2(3.0,4.0);
Distance d;
cout<
return 0;
}