c++用类中的成员函数做友元函数

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
//计算两点之间距离
#include<iostream>
#include<MATH.H>
using namespace std;
class Point;
class test
{
public:
    double dist(Point &p1,Point &p2);
};
class Point
{
 
private:
    int x,y;
public:
    Point(int xx=0,int yy=0)
    {
        x=xx;
        y=yy;
    }
    void display()
    {
        cout<<"("<<x<<","<<y<<")";
    }
    friend double test::dist(Point &p1,Point &p2);
 
};
double test::dist(Point &p1,Point &p2)
{
        return sqrt(double((p1.x-p2.x)*(p1.x-p2.x)+(p1.y-p2.y)*(p1.y-p2.y)));
}
int main()
{
    Point p1(3,4),p2(4,5);
    p1.display();
    cout<<"----->";
    p2.display();
    test a;
    cout<<"距离:"<<a.dist(p1,p2)<<endl;
    return 0;
}

  

  

c++用类中的成员函数做友元函数

上一篇:C++回顾之const对象、const成员函数、mutable类型


下一篇:C++的反射