#include <iostream>
#include <cmath>
using namespace std;
class Point
{
public:
Point (float a,float b)
{
x = a;
y = b;
}
void printf();
float getX() const;
float getY() const;
friend Point middle(const Point &p1,const Point &p2);//友元函数
private:
float x;
float y;
};
//友元函数没有this指针,它可以直接访问Point类中的public、private成员
Point middle(const Point &p1,const Point &p2)
{
cout << "学校坐标为:" << "(" << p1.getX()/2 << "," << p2.getY()/2 << ")" << endl;
}
void Point::printf()
{
cout << "小明家坐标位置(0,0)" << endl;
}
//计算距离
float dist(Point &p1, Point &p2)
{
float distance = sqrt(p1.getX()*p1.getX() + p2.getY()*p2.getY());
cout << "小明家与小志家距离为:" << distance << endl;
}
//获取X坐标
float Point::getX() const
{
return x;
}
//获取Y坐标
float Point::getY() const
{
return y;
}
int main()
{
float a ,b;
cout << "请您输入小志的坐标: " ;
cin >> a >>b;
Point point(a,b);
point.printf();
cout << "小志家坐标为:" << "(" << a << "," << b << ")"<< endl;
middle(point,point);
dist(point,point);
return 0;
}
要是看不懂的话,可以问我
#include <iostream>
#include <math>
using namespace std;
class point
{
public:
point(int xx=0,int yy=0){X=xx;Y=yy;}
int getX(){return X;}
int getY(){return Y;}
friend float dist(point &a, point &b);
private:
int X,Y;
};
float dist(point &p1,point &p2)
{
double x=double(p1.X-p2.X);
double y=double(p1.Y-p2.Y);
return float(sqrt(x*x+y*y));
}
int main()
{
point myp1(0,0),myp2(4,4);
cout<<"距离是:"<< dist(myp1,myp2) << endl;
}