早教吧 育儿知识 作业答案 考试题库 百科 知识分享

(1)为类Point实现一个重载的赋值运算符。(2)为类Point实现重载的流插入运算符。(3)为类Point实现重载的比较关系运算符==和!=。(4)为类Point实现重载的加法+和减法运算符-。(5)实现

题目详情
(1)为类Point实现一个重载的赋值运算符。 (2)为类Point实现重载的流插入运算符。
(3)为类Point实现重载的比较关系运算符==和!=。
(4)为类Point实现重载的加法+和减法运算符-。
(5)实现一个重载的乘法运算符*来返回两个Point类对象的点积。
▼优质解答
答案和解析
刚帮别人写过,你们是一起的吧。。。。。
主函数就不帮你写了。
class Point
{
friend ostream &operator Point(int x=0, int y=0);
Point(const Point &);
bool operator==(const Point &) const;
bool operator!=(const Point &) const;
Point operator=(const Point &);
Point operator+=(const Point &);
Point operator-=(const Point &);
Point operator+(const Point &) const;
Point operator-(const Point &) const;
Point operator*(const Point &) const;
private:
int x;
int y;
};
Point::Point (int a, int b)
{
x = a;
y = b;
}
Point::Point(const Point &a)
{
x = a.x;
y = a.y;
}
bool Point::operator==(const Point &right)const
{
if( x == right.x && y == right.y)
return true;
else return false;
}
bool Point::operator!=(const Point &right) const
{
if( x == right.x && y == right.y)
return false;
else return true;
}
Point Point::operator+=(const Point &right)
{
x += right.x;
y += right.y;
return *this;
}
Point Point::operator-=(const Point &right)
{
x -= right.x;
y -= right.y;
return *this;
}
Point Point::operator=(const Point &right)
{
x = right.x;
y = right.y;
return *this;
}
Point Point::operator+(const Point &right)const
{
Point plus;
plus.x = x + right.x;
plus.y = y + right.y;
return plus;
}
Point Point::operator-(const Point &right)const
{
Point minus;
minus.x = x - right.x;
minus.y = y - right.y;
return minus;
}
Point Point::operator*(const Point &right)const
{
Point multi;
multi.x = x * right.x;
multi.y = y * right.y;
return multi;
}
ostream &operator<{
output << "(" << right.x << "," << right.y << ")"< return output;
}