已知矩形的两点求面积(类的组合)
#include "cmath"#include "iostream"
using namespace std;
class CPoint
{
public:
CPoint(){cout<<"CPoint类的无参构造函数被调用"<<endl;}
CPoint(int xx,int yy);
CPoint(CPoint &p);
~CPoint(){cout<<"CPoint类的析构函数被调用"<<endl;}
getx();
gety();
private:
int x;
int y;
};
CPoint::CPoint(int xx,int yy)
{
x=xx;
y=yy;
cout<<"CPoint类的有参构造函数被调用"<<endl;
}
CPoint::CPoint(CPoint &p)
{
x=p.x;
y=p.y;
cout<<"CPoint类的复制构造函数被调用"<<endl;
}
CPoint::getx()
{
return x;
}
CPoint::gety()
{
return y;
}
class CRect
{
public:
CRect(){cout<<"CRect类的无参构造函数被调用"<<endl;}
CRect(CPoint xp1,CPoint xp2);
CRect(int x,int y,int m,int n);
CRect(CRect &1);
~CRect(){cout<<"CRect类的析构函数被调用"<<endl;}
getArea();
private:
int x;
int y;
int m;
int n;
CPoint p1,p2;
int Area;
};
CRect::CRect(CPoint xp1,CPoint xp2):p1(xp1),p2(xp2)
{
cout<<"CRect类的有参构造函数被调用"<<endl;
int x=static_cast<int>(p1.getx()-p2.getx());
int y=static_cast<int>(p1.gety()-p2.gety());
int Area=x*y;
}
CRect::CRect(int x,int y,int m,int n)
{
this->x=x;
this->y=y;
this->m=m;
this->n=n;
cout<<"CRect类的有4参构造函数被调用"<<endl;
}
CRect::CRect(CRect &1):p1(1.p1),p2(1.p2)
{
cout<<"CRect类的复制构造函数被调用"<<endl;
Area=1.Area;
x=1.x;
y=1.y;
m=1.m;
n=1.n;
}
CRect::getArea()
{
return Area;
}
void main()
{
int a,b,c,d;
cout<<"input a,b,c,d:";
cin>>a>>b>>c>>d>>endl;
CPoint myp1(a,b),myp2(c,d);
CRect rect1,rect2(myp1,myp2),rect3(a,b,c,d),rect4(rect2);
cout<<"rect1面积为:"<<rect1.getArea()<<endl;
cout<<"rect2面积为:"<<rect2.getArea()<<endl;
cout<<"rect3面积为:"<<rect3.getArea()<<endl;
cout<<"rect4面积为:"<<rect4.getArea()<<endl;
}