作业帮 > 综合 > 作业

编写一个CRect类,代表一个矩形,要求CRect类中有代表矩形的左上角坐标(x1,y1)和右下角坐标(x2,y2)的数

来源:学生作业帮 编辑:灵鹊做题网作业帮 分类:综合作业 时间:2024/04/29 03:18:51
编写一个CRect类,代表一个矩形,要求CRect类中有代表矩形的左上角坐标(x1,y1)和右下角坐标(x2,y2)的数据成员,
并可以通过构造器为坐标赋初值,还要求CRect类中有两个成员函数RectHeight()和RectWidth(),
通过这两个函数能得到矩形的高和宽.
附加题:
(1)计算两个矩形的面积之和并显示;
(2)设一个矩形为my_rec变量,能通过cout
编写一个CRect类,代表一个矩形,要求CRect类中有代表矩形的左上角坐标(x1,y1)和右下角坐标(x2,y2)的数
Rect.h
using namespace std;
class CRect
{
public:
CRect(int l,int t,int r,int b);
virtual ~CRect();
protected:int x1;
int y1;
int x2;
int y2;
public: int RectHeight();
int RectWidth();
int AddArea(CRect &r);
friend void operator x1=l;
this->y2=t;
this->x2=r;
this->y2=b;
}
CRect::~CRect()
{
}
int CRect::RectHeight()
{
return y2-y1;
}
int CRect::RectWidth()
{
return x2-x1;
}
int CRect::AddArea(CRect &r)
{
return this->RectHeight()*RectWidth()+r.RectHeight()*r.RectWidth();
}
void operato