设计一个用来表示直角坐标系的Location类,在主程序中创建类Location的两个对象A和B,要求A的坐标在第3象限,B的坐标点在第2象限,分别采用成员函数和友元函数计算给定两个坐标点之间的距离
#include<iostream>
#include<cmath>
using namespace std;
class Location //声明类Location
{
public:
Location(double a,double b); //构造函数
double Getx(){return x;} //成员函数,取x坐标的值
double Gety(){return y;} //成员函数,取y坐标的值
double distance1(Location&);//成员函数,求两坐标点之间的距离
friend double distance2(Location &a, Location &b);//友元函数,求两坐标点之间的距离
private:
double x,y;
};
Location::Location(double a,double b): x(a),y(b){
}
double distance2(Location &a, Location &b){
double dis;
dis = pow(pow(a.x-b.x,2)+pow(a.y-b.y,2),0.5);
cout << dis << endl;
}
int main(void)
{
double a,b,c,d;
cin >> a >> b >> c >> d;
Location location1(a,b);
Location location2(c,d);
location1.Getx();
location1.Gety();
location2.Getx();
location2.Gety();
distance2(location1,location2);
return 0;
}