定义并实现一个矩形类Rectangle,其属性为矩形的左下角与右上角两个点的坐标,能设置左下角和右上角两个点的位置,能根据左下角与右上角两个点的坐标计算矩形的长、宽、周长和面积。要求用类的组合来实现。
方式一:
#include
#include
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:("<>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:"<
方式二:
#include
#include
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:("<>RTX>>RTY;PRT.SetX(RTX);PRT.SetY(RTY);
}
int main()
{Rectangle r1;cout<<"No. 1 rectangle:"<
本文来自互联网用户投稿,文章观点仅代表作者本人,不代表本站立场,不承担相关法律责任。如若转载,请注明出处。 如若内容造成侵权/违法违规/事实不符,请点击【内容举报】进行投诉反馈!
