C++ 比较运算符的重载代码

来源:百度知道 编辑:UC知道 时间:2024/05/11 01:35:20
题目:在POINT类中完成比较运算符==、(自减)运算符(包括前后缀)的重载。

class point
{public:
Point(float x=0,float y=0,float z=0);x_(x),y_(y),z_(z){}
Point(coust Point&p);x_(p.x_),y_(p.y_),z_(p.z_) {}
friend point operator==(const Point&pl,const Point&p2);
[(1)(自减运算符)--(前缀)的重载代码]
[(2)(自减运算符)--(后缀)的重载代码]
private;
float x_,y_,z_;
}
[(3)比较运算符==的重载代码]

考试题的最后一道题大题啊,请求高手帮忙解决。高手赠送,如答案准确,另赠送Q 币10个。

写了下
在我的编译器下你给的代码有点问题...
所以我改了下~
可以用
---------------------------

#include<iostream>

using namespace std;

class Point
{
public:
Point(const Point&p)
{
x_=p.x_;y_=p.y_;z_=p.z_;
}

Point(float x=0,float y=0,float z=0)
{
x_=x;y_=y;z_=z;
}

bool operator==(const Point &p1)//比较运算符==的重载代码
{
if (this->x_==p1.x_&&this->y_==p1.y_&&this->z_==p1.z_)
{
return true;
}
else
return false;
}

Point& operator++() //(自减运算符)--(后缀)的重载代码
{
// objtype对象自增操作
this->x_++;
this->y_++;
this->z_++;
return *this;
}

Point& operator++(int noused) //自减运算符)--(前缀)的重载代码
{
Point *tmp = new Point(*this);
this->x_++;
this->y_++;
this->z_++;
return *tmp;
}

private:
float x_,y_,z_;
};

int