定义并实现一个矩形类Rectangle,其属性为矩形的左下角与右上角两个点的坐标,能设置左下角和右上角两个点的位置,能根据左下角与右上角两个点的坐标计算矩形的长、宽、周长和面积。要求用类的组合来实现。

该代码示例展示了如何使用C++定义一个`Point`类和一个`Rectangle`类。`Point`类包含坐标点的定义,而`Rectangle`类则包含了矩形的左下角和右上角点,以及矩形的长度和宽度计算、周长和面积计算等功能。此外,还提供了设置矩形边界点和显示矩形信息的方法。
摘要由CSDN通过智能技术生成

方式一:

#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;
}

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值