定义一个表示三维空间坐标点的类,并对“>”运算符重载,如果A点到原点的距离大于B点到原点的距离,则A>B为

真,否则为假。用C++编写
2025-04-09 04:24:05
推荐回答(1个)
回答1:

#include
#include
using namespace std;
class Points
{
private:
double x;
double y;
double z;
public:
Points(){x=0.0;y=0.0;z=0.0;}
Points(double x,double y,double z)
{
this->x=x;
this->y=y;
this->z=z;
}
bool operator>(Points &a)
{
return sqrt(pow(this->x,2)+pow(this->y,2)+pow(this->z,2))>sqrt(pow(a.x,2)+pow(a.y,2)+pow(a.z,2));
}
};
int main()
{
Points a(1.0,2.0,3.0);
Points b(3.0,4.0,5.0);
bool k=a>b;
cout< return 0;
}