首页
学习
活动
专区
圈层
工具
发布
首页
学习
活动
专区
圈层
工具
MCP广场
社区首页 >问答首页 >重载运算符以返回区域

重载运算符以返回区域
EN

Stack Overflow用户
提问于 2020-11-21 14:19:20
回答 1查看 67关注 0票数 0

我正在尝试重载2个运算符。我想重载'*‘操作符来返回两个相交矩形的面积。然后,我尝试重载'+‘运算符,以求矩形面积减去相交面积的总和。

我试图使用朋友函数重载'*‘运算符,但它显示参数太多。有什么办法可以绕过这一点吗?或者有其他方法可以做到这一点?

point.h

代码语言:javascript
运行
复制
#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

代码语言:javascript
运行
复制
#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

代码语言:javascript
运行
复制
#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

代码语言:javascript
运行
复制
#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)
}
EN

回答 1

Stack Overflow用户

发布于 2020-11-21 14:28:36

考虑一下你的函数签名。

代码语言:javascript
运行
复制
double operator+(const Rectangles& r, const Point _point1, const Point _point2)

表达式中的参数是什么?您将像这样使用运算符重载:

代码语言:javascript
运行
复制
Rectangles r1, r2;
...
auto overlap = r1+r2;

因此,唯一合理的operator+不能接受3个变量,它必须只接受2个变量。这2个变量必须是您执行操作所需的全部。例如:

代码语言:javascript
运行
复制
double operator+(const Rectangles& r1, const Rectangles& r2);

此外,在您的friend函数(不是类成员)中,您可以这样做:

代码语言:javascript
运行
复制
this->computeArea

这里的this是什么?您不在成员函数中,因此没有this

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

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

这些函数中的每一个都可能在您的shape界面中公开可用,所以一个不是朋友的operator+应该是这样的:

代码语言:javascript
运行
复制
double operator+(const Shape& s1, const Shape& s2) {
    return s1.area() + s2.area() + intersection(s1, s2).area();
}
票数 1
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/64940404

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档