P120.3:
实验3: 设计一个用来表示直角坐标系的LOCATION类,在主程序中创建类LOCATION的两个对象A和B,要求A的坐标点在第3象限,B的坐标点在第2象限,分别采用成员函数和友元函数计算给定两个坐标点之间的距离。
#include<iostream>
#include<cmath>
using namespace std;
class Location{
public:
double a,b,d1,d2;
Location(double a ,double b);
double getx();
double gety();
double distance(Location&,Location&);
friend double distance1(Location&,Location&);
private:
double x,y;
};
Location::Location(double a,double b)
{ x=a;
y=b;
}
double Location:: getx()
{ return x;
}
double Location:: gety()
{ return y;
}
double Location::distance(Location&c,Location&d)
{ d1=sqrt((c.x-d.x)*(c.x-d.x)+(c.y-d.y)*(c.y-d.y));
cout<<"Distance1="<<d1<<endl;
return 0;}
double distance1(Location& c,Location& d)
{ double d2;
d2=sqrt((c.x-d.x)*(c.x-d.x)+(c.y-d.y)*(c.y-d.y));
cout<<"Distance2=