方式一:
#include <iostream>
#include <stdlib.h>
using namespace std;
class Point //Point类声明
{
public:
Point(float xx=0, float yy=0) {X=xx;Y=yy;}
Point(Point &p);
float GetX() {return X;}
float GetY() {return Y;}
void SetX(float xx) { X=xx;}
void SetY(float yy) { Y=yy;}
private:
float X,Y;
};
Point::Point(Point &p) //拷贝构造函数的实现
{
X=p.X;
Y=p.Y;
}
class Rectangle
{
private:
Point PLB,PRT;
float length,width;
public:
Rectangle();
Rectangle(float x1,float y1,float x2,float y2);
Rectangle(Point p1,Point p2);
Rectangle(Rectangle &r);
float getlength();
float getwidth();
float area();
float crect();
void SetLB();
void SetRT();
void show();
};
Rectangle::Rectangle()
{
length=0;width=0;
}
Rectangle::Rectangle(float x1,float y1,float x2,float y2):PLB(x1,y1),PRT(x2,y2)
{
length=x2-x1;
width=y2-y1;
}
Rectangle::Rectangle(Point p1,Point p2):PLB(p1),PRT(p2)
{
length=PRT.GetX()-PLB.GetX();
width=PRT.GetY()-PLB.GetY();
}
Rectangle::Rectangle(Rectangle &r):PLB(r.PLB),PRT(r.PRT),length(r.length),width(r.width)
{
}
float Rectangle::getlength()
{
return length;
}
float Rectangle::getwidth()
{
return width;
}
float Rectangle::area()
{
return length*width;
}
float Rectangle::crect()
{
return 2*(length+width);
}
void Rectangle::SetLB()
{
float LBX,LBY;
cout<<"Please enter left bottom point:";
cin>>LBX>>LBY;
PLB.SetX(LBX);
PLB.SetY(LBY);
length=PRT.GetX()-PLB.GetX();
width=PRT.GetY()-PLB.GetY();
}
void Rectangle::show()
{
cout<<"left bottom point:("<<PLB.GetX()<<","<<PLB.GetY()<<")"<<endl;
cout<<"right top point:("<<PRT.GetX()<<","<<PRT.GetY()<<")"<<endl;
}
void Rectangle::SetRT()
{
float RTX,RTY;
cout<<"Please enter right top point:";
cin>>RTX>>RTY;
PRT.SetX(RTX);
PRT.SetY(RTY);
length=PRT.GetX()-PLB.GetX();
width=PRT.GetY()-PLB.GetY();
}
int main()
{
Rectangle r1;
cout<<"No. 1 rectangle:"<<endl;
r1.show();
cout<<"长:"<<r1.getlength()<<endl;
cout<<"宽:"<<r1.getwidth()<<endl;
cout<<"面积:"<<r1.area()<<endl;
cout<<"周长:"<<r1.crect()<<endl;
cout<<endl<<endl;
r1.SetLB();
r1.SetRT();
r1.show();
cout<<"长:"<<r1.getlength()<<endl;
cout<<"宽:"<<r1.getwidth()<<endl;
cout<<"面积:"<<r1.area()<<endl;
cout<<"周长:"<<r1.crect()<<endl;
Point plb(2,2),prt(10,8);
Rectangle r2(plb,prt);
cout<<"No. 2 rectangle:"<<endl;
r2.show();
cout<<"长:"<<r2.getlength()<<endl;
cout<<"宽:"<<r2.getwidth()<<endl;
cout<<"面积:"<<r2.area()<<endl;
cout<<"周长:"<<r2.crect()<<endl;
cout<<endl<<endl;
Rectangle r3(0,0,10,10);
cout<<"No. 3 rectangle:"<<endl;
r3.show();
cout<<"长:"<<r3.getlength()<<endl;
cout<<"宽:"<<r3.getwidth()<<endl;
cout<<"面积:"<<r3.area()<<endl;
cout<<"周长:"<<r3.crect()<<endl;
cout<<endl<<endl;
Rectangle r4(r1);
cout<<"No. 4 rectangle:"<<endl;
r4.show();
cout<<"长:"<<r4.getlength()<<endl;
cout<<"宽:"<<r4.getwidth()<<endl;
cout<<"面积:"<<r4.area()<<endl;
cout<<"周长:"<<r4.crect()<<endl;
cout<<endl<<endl;
system("pause");
return 0;
}
方式二:
#include <iostream>
#include <stdlib.h>
using namespace std;
class Point //Point类声明
{
public:
Point(float xx=0, float yy=0) {X=xx;Y=yy;}
Point(Point &p);
float GetX() {return X;}
float GetY() {return Y;}
void SetX(float xx) { X=xx;}
void SetY(float yy) { Y=yy;}
private:
float X,Y;
};
Point::Point(Point &p) //拷贝构造函数的实现
{
X=p.X;
Y=p.Y;
}
class Rectangle
{
private:
Point PLB,PRT;
public:
Rectangle();
Rectangle(float x1,float y1,float x2,float y2);
Rectangle(Point p1,Point p2);
Rectangle(Rectangle &r);
float length();
float width();
float area();
float crect();
void SetLB();
void SetRT();
void show();
};
Rectangle::Rectangle()
{
}
Rectangle::Rectangle(float x1,float y1,float x2,float y2):PLB(x1,y1),PRT(x2,y2)
{
}
Rectangle::Rectangle(Point p1,Point p2):PLB(p1),PRT(p2)
{
}
Rectangle::Rectangle(Rectangle &r):PLB(r.PLB),PRT(r.PRT)
{
}
float Rectangle::length()
{
return PRT.GetX()-PLB.GetX();
}
float Rectangle::width()
{
return PRT.GetY()-PLB.GetY();
}
float Rectangle::area()
{
return length()*width();
}
float Rectangle::crect()
{
return 2*(length()+width());
}
void Rectangle::SetLB()
{
float LBX,LBY;
cout<<"Please enter left bottom point:";
cin>>LBX>>LBY;
PLB.SetX(LBX);
PLB.SetY(LBY);
}
void Rectangle::show()
{
cout<<"left bottom point:("<<PLB.GetX()<<","<<PLB.GetY()<<")"<<endl;
cout<<"right top point:("<<PRT.GetX()<<","<<PRT.GetY()<<")"<<endl;
}
void Rectangle::SetRT()
{
float RTX,RTY;
cout<<"Please enter right top point:";
cin>>RTX>>RTY;
PRT.SetX(RTX);
PRT.SetY(RTY);
}
int main()
{
Rectangle r1;
cout<<"No. 1 rectangle:"<<endl;
r1.show();
cout<<"长:"<<r1.length()<<endl;
cout<<"宽:"<<r1.width()<<endl;
cout<<"面积:"<<r1.area()<<endl;
cout<<"周长:"<<r1.crect()<<endl;
cout<<endl<<endl;
r1.SetLB();
r1.SetRT();
r1.show();
cout<<"长:"<<r1.length()<<endl;
cout<<"宽:"<<r1.width()<<endl;
cout<<"面积:"<<r1.area()<<endl;
cout<<"周长:"<<r1.crect()<<endl;
Point plb(2,2),prt(10,8);
Rectangle r2(plb,prt);
cout<<"No. 2 rectangle:"<<endl;
r2.show();
cout<<"长:"<<r2.length()<<endl;
cout<<"宽:"<<r2.width()<<endl;
cout<<"面积:"<<r2.area()<<endl;
cout<<"周长:"<<r2.crect()<<endl;
cout<<endl<<endl;
Rectangle r3(0,0,10,10);
cout<<"No. 3 rectangle:"<<endl;
r3.show();
cout<<"长:"<<r3.length()<<endl;
cout<<"宽:"<<r3.width()<<endl;
cout<<"面积:"<<r3.area()<<endl;
cout<<"周长:"<<r3.crect()<<endl;
cout<<endl<<endl;
Rectangle r4(r1);
cout<<"No. 4 rectangle:"<<endl;
r4.show();
cout<<"长:"<<r4.length()<<endl;
cout<<"宽:"<<r4.width()<<endl;
cout<<"面积:"<<r4.area()<<endl;
cout<<"周长:"<<r4.crect()<<endl;
cout<<endl<<endl;
system("pause");
return 0;
}