标题:公有成员函数与友元函数的转换
取消只看楼主
魔鬼鱼
Rank: 1
等 级:新手上路
帖 子:6
专家分:0
注 册:2011-3-25
结帖率:100%
 问题点数:0 回复次数:0 
公有成员函数与友元函数的转换
#include <stdafx.h>
#include <iostream.h>
#include <cmath>
class Point
{
       double x;
       double y;
public:
       Point(double a,double b)
       {
              x=a;
              y=b;
       }
       friend double dist(Point a,Point b);
};
double dist(Point a,Point b)
{
     return sqrt((a.x-b.x)*(a.x-b.x)+(a.y-b.y)*(a.y-b.y));
}
void main()
{
     Point p1(1,2);
     Point p2(5,2);
     cout<<dist(p1,p2)<<endl;
}

将友元函数dist(point a,point b)改为Point类的公有成员函数 Point ::dist(point &b),请修改主函数中的相应代码时程序功能不变。





搜索更多相关主题的帖子: return 
2011-05-09 20:26



参与讨论请移步原网站贴子:https://bbs.bccn.net/thread-339334-1-1.html




关于我们 | 广告合作 | 编程中国 | 清除Cookies | TOP | 手机版

编程中国 版权所有,并保留所有权利。
Powered by Discuz, Processed in 0.271945 second(s), 8 queries.
Copyright©2004-2025, BCCN.NET, All Rights Reserved