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