我正在尝试重载2个运算符。我想重载'*‘操作符来返回两个相交矩形的面积。然后,我尝试重载'+‘运算符,以求矩形面积减去相交面积的总和。
我试图使用朋友函数重载'*‘运算符,但它显示参数太多。有什么办法可以绕过这一点吗?或者有其他方法可以做到这一点?
point.h
#ifndef POINT_H
#define POINT_H
class Point
{
int x;
int y;
public:
Point(int = 0, int = 0);
int getX();
int getY();
void setX(int);
void setY(int);
};
#endif
point.cpp
#include "point.h"
Point::Point(int xcoor, int ycoor)
{
x = xcoor;
y = ycoor;
}
int Point::getX()
{
return x;
}
int Point::getY()
{
return y;
}
void Point::setX(int xcoor)
{
x = xcoor;
}
void Point::setY(int ycoor)
{
y = ycoor;
}
rectangletestdriver.h
#ifndef RECTANGLETESTDRIVER_H
#define RECTANGLETESTDRIVER_H
#include "shape.h"
#include "point.h"
#include <iostream>
#include <string>
class Rectangles : public Shape
{
public:
int x1;
int y1;
int x2;
int y2;
Rectangles();
Rectangles(Point, Point);
Point point;
int getX1(Point _point);
int getY1(Point _point);
int getX2(Point _point);
int getY2(Point _point);
int getWidth(Point _point1, Point _point2);
int getHeight(Point _point1, Point _point2);
double computeArea(Point _point1, Point _point2);
bool overlap(Rectangles _rectangle1, Rectangles _rectangle2, Point _point1, Point _point2, Point _point3, Point _point4);
double intersectedArea(Rectangles _rectangle1, Rectangles _rectangle2, Point _point1, Point _point2, Point _point3, Point _point4);
friend double operator+ (const Rectangles& r, const Point _point1, const Point _point2);
};
#endif
rectangletestdriver.cpp
#include "rectangletestdriver.h"
#include "point.h"
#include <iostream>
using namespace std;
Rectangles::Rectangles()
{
}
Rectangles::Rectangles(Point _point1, Point _point2)
{
int x1 = _point1.getX();
int y1 = _point1.getY();
int x2 = _point2.getX();
int y2 = _point2.getY();
}
int Rectangles::getX1(Point _point1)
{
return _point1.getX();
}
int Rectangles::getY1(Point _point1)
{
return _point1.getY();
}
int Rectangles::getX2(Point _point2)
{
return _point2.getX();
}
int Rectangles::getY2(Point _point2)
{
return _point2.getY();
}
int Rectangles::getWidth(Point _point1, Point _point2)
{
return _point2.getX() - _point1.getX();
}
int Rectangles::getHeight(Point _point1, Point _point2)
{
return _point2.getY() - _point1.getY();
}
double Rectangles::computeArea(Point _point1, Point _point2)
{
double area = (this->getHeight(_point1, _point2) * 2) + (this->getWidth(_point1, _point2) * 2);
return area;
}
bool Rectangles::overlap(Rectangles _rectangle1, Rectangles _rectangle2, Point _point1, Point _point2, Point _point3, Point _point4)
{
if ((_rectangle1.getX1(_point1) >= _rectangle2.getX2(_point4) || _rectangle2.getX1(_point3) >= _rectangle1.getX2(_point2)) && (_rectangle1.getY1(_point1) <= _rectangle2.getY2(_point4) || _rectangle2.getY1(_point3) <= _rectangle1.getY2(_point2)))
{
cout << "no overlap" << endl;
return false;
}
else
{
cout << "overlap" << endl;
}
}
double Rectangles::intersectedArea(Rectangles _rectangle1, Rectangles _rectangle2, Point _point1, Point _point2, Point _point3, Point _point4)
{
int x_dist = min(_rectangle1.getX2(_point2), _rectangle2.getX2(_point4)) - max(_rectangle1.getX1(_point1), _rectangle2.getX1(_point3));
int y_dist = min(_rectangle1.getY2(_point2), _rectangle2.getY2(_point4)) - max(_rectangle1.getY1(_point1), _rectangle2.getY1(_point3));
int area;
area = x_dist * y_dist;
return area;
}
double operator+(const Rectangles& r, const Point _point1, const Point _point2)
{
Rectangles rectangles;
rectangles.computeArea(_point1, _point2) = this->computeArea + rectangles.computeArea(_point1, _point2)
}
发布于 2020-11-21 14:28:36
考虑一下你的函数签名。
double operator+(const Rectangles& r, const Point _point1, const Point _point2)
表达式中的参数是什么?您将像这样使用运算符重载:
Rectangles r1, r2;
...
auto overlap = r1+r2;
因此,唯一合理的operator+
不能接受3个变量,它必须只接受2个变量。这2个变量必须是您执行操作所需的全部。例如:
double operator+(const Rectangles& r1, const Rectangles& r2);
此外,在您的friend函数(不是类成员)中,您可以这样做:
this->computeArea
这里的this
是什么?您不在成员函数中,因此没有this
。
关于设计的最后一点是,为什么你需要operator+
做朋友?两个形状的并集是什么?它是:
面积(R1)+面积(R2)-面积(交点(r1,r2))
这些函数中的每一个都可能在您的shape界面中公开可用,所以一个不是朋友的operator+
应该是这样的:
double operator+(const Shape& s1, const Shape& s2) {
return s1.area() + s2.area() + intersection(s1, s2).area();
}
https://stackoverflow.com/questions/64940404
复制相似问题