友元函数,友元成员,友元类
#include<iostream>
using namespace std;
#include<cmath>
class point {
int x, y;
public:
point(int a = 0, int b = 0)
{
x = a;
y = b;
}
void show()
{
cout << "x=" << x << " y=" << y << endl;
}
};
double Distance(point &p1,point &p2)
{
double d;
d = sqrt((p2.x - p1.x) * (p2.x - p1.x) + (p2.y - p1.y) * (p2.y - p1.y));
return d;
}
int main()
{
point p1, p2(1, 1);
return 0;
}
为保护隐藏数据,x,y是私有的。此时访问数据只能另设函数。
#include<iostream>
using namespace std;
#include<cmath>
class point {
int x, y;
public:
point(int a = 0, int b = 0)
{
x = a;
y = b;
}
void show()
{
cout << "x=" << x << " y=" << y << endl;
}
int getx()
{
return x;
}
int gety()
{
return y;
}
};
double Distance(point &p1,point &p2)
{
double d;
d = sqrt((p2.getx() - p1.getx()) * (p2.getx() - p1.getx()) + (p2.gety() - p1.gety()) * (p2.gety() - p1.gety()));
return d;
}
int main()
{
point p1, p2(1, 1);
cout << Distance(p1, p2);//计算p1和p2两点之间的距离
return 0;
}
但算一个d,要调用很多次getx,gety。
于是我们提出友元机制。
#include<iostream>
using namespace std;
#include<cmath>
class point {
int x, y;
public:
point(int a = 0, int b = 0)
{
x = a;
y = b;
}
void show()
{
cout << "x=" << x << " y=" << y << endl;
}
int getx()
{
return x;
}
int gety()
{
return y;
}
friend double Distance(point& p1, point& p2);
};
double Distance(point &p1,point &p2)
{
double d;
d = sqrt((p2.x- p1.x) * (p2.x - p1.x) + (p2.y - p1.y) * (p2.y - p1.y));
return d;
}
int main()
{
point p1, p2(1, 1);
cout << Distance(p1, p2);
return 0;
}
一个类的成员函数也可以作为另一个类的友元。
#include<iostream>
using namespace std;
class girl;
class boy
{
string name;
public:
boy(string n)
{
name = n;
}
void introduceoneself()
{
cout << "I am " << name << endl;
}
void introducefriend(girl &x);
};
class girl
{
string name;
public:
girl(string n)
{
name = n;
}
void introduceoneself()
{
cout << "I am " << name << endl;
}
friend void boy::introducefriend(girl &x);
//声明boy的成员函数introducefriend()是girl的友元函数,这样就能访问girl里面的私有x。
};
void boy::introducefriend(girl &x)
{
cout << "She is " << x.name << endl;
}
int main()
{
boy boy1("Tom");
girl girl1("Marry");
boy1.introduceoneself();
boy1.introducefriend(girl1);
return 0;
}
一个类也可以作为一个类的友元:此时该类中的所有成员函数都是另一个类的友元成员,都可以访问另一个类的所有成员。
还是以上例子,男孩若想介绍第二个女孩,再次使用这种方法(以下代码)就有点浪费代码段。。
#include<iostream>
using namespace std;
class girl;
class boy
{
string name;
public:
boy(string n)
{
name = n;
}
void introduceoneself()
{
cout << "I am " << name << endl;
}
void introducefriend1(girl &x);
void introducefriend2(girl& x);
};
class girl
{
string name;
public:
girl(string n)
{
name = n;
}
void introduceoneself()
{
cout << "I am " << name << endl;
}
friend void boy::introducefriend1(girl &x);
friend void boy::introducefriend2(girl& x);
//声明boy的成员函数introducefriend()是girl的友元函数,这样就能访问girl里面的私有x。
};
void boy::introducefriend1(girl &x)
{
cout << "She is " << x.name << endl;
}
void boy::introducefriend2(girl& x)
{
cout << x.name<<" is my best friend " << endl;
}
int main()
{
boy boy1("Tom");
girl girl1("Marry");
girl girl2("Lily");
boy1.introduceoneself();
boy1.introducefriend1(girl1);
boy1.introducefriend2(girl1);
return 0;
}
于是我们可以直接声明友元类。(如下)声明男孩类是女孩类的朋友。这时女孩类里面的所有成员都是男孩类的朋友。
#include<iostream>
using namespace std;
class girl;
class boy
{
string name;
public:
boy(string n)
{
name = n;
}
void introduceoneself()
{
cout << "I am " << name << endl;
}
void introducefriend1(girl &x);
void introducefriend2(girl& x);
};
class girl
{
string name;
friend boy;
public:
girl(string n)
{
name = n;
}
void introduceoneself()
{
cout << "I am " << name << endl;
}
};
void boy::introducefriend1(girl &x)
{
cout << "She is " << x.name << endl;
}
void boy::introducefriend2(girl& x)
{
cout << x.name<<" is my best friend " << endl;
}
int main()
{
boy boy1("Tom");
girl girl1("Marry");
girl girl2("Lily");
boy1.introduceoneself();
boy1.introducefriend1(girl1);
boy1.introducefriend2(girl1);
return 0;
}
说明:
友元关系是单向的,不具有交换性,在类A中将类B声明为自己的友元,但类B中没有将类A声明为友元时,A的成员函数不可以访问B的私有成员。
当两个类都将对方声明为自己的友元时,才可以实现互访
友元关系也不具备传递性,A将B声明为友元,B将C声明为友元,但C不一定是A的友元