【问题标题】:Overloading an operator to return areas重载运算符以返回区域
【发布时间】:2021-03-04 12:21:08
【问题描述】:

我正在尝试重载 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

矩形测试驱动程序.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)
}

【问题讨论】:

  • operator+ 需要三个参数?你能举一个例子来说明你是如何想象这种外观的吗?
  • 在谈论运算符重载之前,您是否意识到您的getX1getX2getY1getY2 做了什么?
  • 从什么时候开始将两个矩形相交的面积表示为这些矩形的乘积?通常,在 C++ 中,操作符重载应该允许对普通人有意义的表达式。并且写rectangle1*rectangle2 来获得这两个矩形的交集区域是混淆的。对于人类来说,一种更易于理解的方法是获取表示矩形交集的形状,并计算该形状的面积。例如rect1.Intersection(rect2).Area()

标签: c++ class operator-overloading operators overloading


【解决方案1】:

想想你的函数签名。

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);

此外,在您的朋友功能(不是类成员)中,您可以这样做:

this->computeArea

这里的this 是什么?你不在成员函数中,所以没有this


关于设计的最后一点,为什么你需要operator+ 成为朋友? 2个形状的并集是什么?它是:

面积(r1) + 面积(r2) - 面积(交点(r1, r2))

这些功能中的每一个都可能在您的形状界面中公开可用,因此不是朋友的operator+ 看起来像这样:

double operator+(const Shape& s1, const Shape& s2) {
    return s1.area() + s2.area() + intersection(s1, s2).area();
}

【讨论】:

  • 即使你在一个成员函数中并且可以写this-&gt;computeArearectangles.ComputeArea(_point1, _point2)是一个prvalue并且不能被赋值。
  • @NathanPierson,是的,这段代码有很多问题。
猜你喜欢
  • 1970-01-01
  • 1970-01-01
  • 2015-01-29
  • 2020-07-09
  • 1970-01-01
  • 1970-01-01
  • 2019-05-12
  • 2015-08-12
  • 1970-01-01
相关资源
最近更新 更多