法一:
#include
#include<math.h>
using namespace std;
class point
{
private:
float x,y;
public:
point(float a,float b){x=a;y=b;}
float getx(){return x;}
float gety(){return y;}
void show(){cout<<“坐标为(”<<x<<","<<y<<")"<<endl;}
};
void main()
{
point A(2.5,3.3),B(3,4.4);
float len;
len=sqrt((A.getx()-B.getx())(A.getx()-B.getx())+(A.gety()-B.gety())(A.gety()-B.gety()));
A.show();
B.show();
cout<<“两点间的距离为:”<<len<<endl;
}
法二:利用友元函数
#include
#include<math.h>
using namespace std;
class point
{
private:
float x,y;
public:
point(float a,float b){x=a;y=b;}
float getx(){return x;}
float gety(){return y;}
void show(){cout<<“坐标为(”<<x<<
设计一个点类,显示横纵坐标并求出两点的距离
最新推荐文章于 2023-08-22 10:16:13 发布