现在的位置: 首页 > 综合 > 正文

MFC中的CRect(区域)

2012年07月30日 ⁄ 综合 ⁄ 共 1190字 ⁄ 字号 评论关闭

写一个CRect类表示一个矩形,该矩形类成员变量为:x1,y1,x2,y2,矩形左上角和右下角的坐标。然后完成以下几个成员函数:
1. CRect(); //无参数的构造函数
2. CRect(double x1_, double y1_, double x2_, double y2_); //带有4个参数的构造函数(矩形左上角和右下角的坐标)
3. bool IsSquare(); //判断该矩形是否是正方形
4. double Area(); //求该矩形的面积
5. bool Intersecting(CRect rect); //求该矩形是否和另一个矩形是否相交,也就是当前矩形和矩形rect有重合部分

 

 

实现:

#include<iostream.h>
class CRect{
private:
double x1;
double x2;
double y1;
double y2;
public:
CRect();
CRect(double x1_, double y1_, double x2_, double y2_);
bool IsSquare();
double Area();
bool Intersecting(CRect rect);
};
CRect::CRect()
{x1=y1=0;
x2=y2=1;
}
CRect::(double x1_, double y1_, double x2_, double y2_)
{x1=x1_;
y1=y1_;
x2=x2_;
y2=y2_;
}
bool CRect::IsSquare()
{if((x2-x1)==(y2-y1))
return true;
else return false;
}
double CRect::Area()
{return ((x2-x1)*(y2-y1));
}
bool Intersecting(CRect rect);
{if(rect.x1<x2||rect.y1<y2||rect.x2>x1||rect.y2>y1)
return true;
else return false;
}
int main()
{double x1,x2,y1,y2;
cout<<"Please input two endpoints:"<<endl;
cin>>x1>>y1>>x2>>y2;
CRect C1(x1,y1,x2,y2);
CRect C2;
if(C1.IsSquare())
cout<<"The rect is a square!"<<endl;
else cout<<"The rect is not a square!"<<endl;
cout<<"The area of the rect is:"<<C1.Area()<<endl;
if(C1.Intersecting(C2))
cout<<"rect C1 intersecting with rect C2!"<<endl;
else cout<<"rect C1 not intersecting with rect C2!"<<endl;
return 0;
}

抱歉!评论已关闭.