C++ 求两点间距离 求高手帮忙纠错

C++ 求两点间距离 求高手帮忙纠错,第1张

和系统函数重名了,把塌空函数名改了就没问题了,以后注意一下namespace的命名空间

#include <iostream>

#include <cmath>

using namespace std

class CPoint

{

public:

friend float Mydistance(CPoint &p1,CPoint &p2)

void set(){cin>>X>>唯燃Y}

private:

int X

int Y

}

float Mydistance(CPoint &p1, CPoint &p2)

{

return (float)sqrt((p1.X - p2.X) * (p1.X - p2.X) + (p1.Y - p2.Y) * (p1.Y - p2.Y))

}

int main()

{

CPoint p1,p2

p1.set()

p2.set()

cout<<"Distance="<<Mydistance(p1,p2)<团山瞎<endl

return 0

}

友元函数在定义和调用上与普通函数一样,不同于成员函数的定晌渣义陪余,它不需芦谨滚要指出所属的类。

#include<iostream.h>

class

Point

{

public:

Point(double

xx,double

yy)

{x=xx,y=uu}

void

Getxy()

friend

double

Distance(Point

&a,Point

&b)

private:

double

x,y

}

void

Point::Getxy()

{

cout<<"("<<x<<","<<y<<")"<<endl

}

double

Distance(Point

&a,Point&b)

{

double

dx=a.x-b.x

double

dy=a.y-b.y

double

sqrt(dx*dy+dy*dy)

}

void

main()

{

Point

p1(3.0,4.0),p2(6.0,8.0)

p1.Getxy()

p2.Getxy()

double

d=Distance(p1,p2)

cout<<"Distance

is"<<d<<endl

}

结果:

(3.0,4.0)

(6.0,8.0)

Distance

is

5


欢迎分享,转载请注明来源:内存溢出

原文地址: http://outofmemory.cn/tougao/12137588.html

(0)
打赏 微信扫一扫 微信扫一扫 支付宝扫一扫 支付宝扫一扫
上一篇 2023-05-21
下一篇 2023-05-21

发表评论

登录后才能评论

评论列表(0条)

保存