首页 > 编程语言 > 详细

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

时间:2014-02-09 15:55:38      阅读:333      评论:0      收藏:0      [点我收藏+]
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++用类中的成员函数做友元函数

原文:http://www.cnblogs.com/sanzang/p/3540929.html

(0)
(0)
   
举报
评论 一句话评论(0
关于我们 - 联系我们 - 留言反馈 - 联系我们:wmxa8@hotmail.com
© 2014 bubuko.com 版权所有
打开技术之扣,分享程序人生!