/*7.定义一个点类Point,包括x坐标和y坐标(int)。
定义一个CRect类,代表一个矩形,
要求CRect类中有代表矩形的左上角坐标(x1,y1)
和右下角坐标(x2,y2)点类的对象,
要求CRect类中有两个成员函数RectHeight()和RectWidth(),
通过这两个函数能得到矩形的高和宽;
此外还需要有求此矩形面积的成员函数。
要求每个类至少有两个以上构造函数,
在main函数中测试他门的每一个成员函数。
注:只考虑四条边和横纵轴垂直平行的矩形。*/
#include <iostream>
#include <math.h>
using namespace std;
/*与7.3相比较 为什么Point类的无参数构造函数无法调用?
应该在CRect的构造函数里面写些什么?*/
class Point{ //定义一个点类Point
public:
Point()
{
x=0;
y=0;
cout<<"calling the constructor"<<endl;
cout<<"(x,y)为"<<"("<<x<<","<<y<")";
}
Point(int xx=0,int yy=0) //Point类的构造函数
{
x=xx;
y=yy;
cout<<"calling the constructor"<<endl;
cout<<"(x,y)为"<<"("<<x<<","<<y<")";
}
int getX(){
return x;
}
int getY(){
return y;
}
private:
int x,y;
};
class CRect{ //定义一个CRect类
public:
CRect(); //无参数的构造函数
CRect(Point p,Point p1); //有参数的构造函数
int RectHeight(); //得到矩形的高
int RectWidth(); //得到矩形的宽
int RectArea(); //得到矩形的面积
private:
Point p,p1;
};
CRect::CRect(Point pp,Point pp1):p(pp),p1(pp1)
{
cout<<"calling the constructor"<<endl;
}
int CRect::RectHeight()
{
int H=0;
H=abs(p.getY()-p1.getY());
return H;
}
int CRect::RectWidth()
{
int W=0;
W=abs(p.getX()-p1.getX());
return W;
}
int CRect::RectArea()
{
int A=0;
A=abs((p.getY()-p1.getY())*(p.getX()-p1.getX()));
return A;
}
int main(int argc, char** argv) {
int x,y;
int x1,y1;
cin>>x>>y;
cin>>x1>>y1;
Point a; //??????
Point p(x,y); //调用Point的构造函数
Point p1(x1,y1);
CRect c(p,p1); //调用CRect的构造函数
int H=0,W=0,A=0;
H=c.RectHeight();
cout<<"矩形的高为"<<H<<endl;
W=c.RectWidth();
cout<<"矩形的宽为"<<W<<endl;
A=c.RectArea();
cout<<"矩形的面积为"<<A<<endl;
return 0;
}