#include <iostream>
#include <math.h>
using namespace std;
/*与7.3相比较 为什么Point类的无参数构造函数都无法调用?
应该在CRect的构造函数里面写些什么?
有没有一种可能能在CRect的构造函数中调用CRect的成员函数?*/
class Point{ //定义一个点类Point
public:
/* Point()
{
x=0;
y=0;
cout<<"calling the constructor"<<endl;
cout<<"(x,y)为"<<"("<<x<<","<<y<")";
}*/
//简化上述代码
Point()
{
Point(0,0);
cout<<"calling the constructor"<<endl;
cout<<"(x,y)为"<<"("<<x<<","<<y<<")"<<endl;
}
Point(int xx,int yy):x(xx),y(yy) //Point类的构造函数
{
/* x=xx;
y=yy;合成到构造函数的声明里面*/
cout<<"calling the constructor"<<endl;
cout<<"(x,y)为"<<"("<<x<<","<<y<<")"<<endl;
}
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()
和
Point(int x=0,int y=0)
两个混淆了。
编译器遇到
Point p不知道是调用p()好,还是p(x=0,y=0)好。*/
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;
}