C++ 设计一个Rectangle类,计算周长,面积,并绘制出来

我们先创建具有属性length(长度)和width(宽度)的类Rectangle(长方形),这两个属性的默认值为1。分别提供计算长方形perimeter(周长)和area(面积)的成员函数。另外,为lengthwidth两个属性提供设置获取函数。设置函数应该验证length和width是大于0.0且小于20.0的浮点数

 以下就是上述的实现代码:

//Rectangle类.cpp 
#include <iostream>
#include <iomanip>//使用了这个头文件中的stew()来设置输出宽度
using namespace std;
class Rectangle
{
	private:
		float length;
		float width;
	public:
		Rectangle()//Initialize in constructor(构造函数初始化数据成员)
		{
			length=1.0;
			width=1.0;
		}
		void setLength( float leng )//Length的设置函数
		{
			if( leng>0 || leng<20 )//保证>0且<20
			{
				length=leng;
			}
			else
			{
				cout << "Length out of range!!" << endl;
			}
		}
		void setWidth( float wid )//Width的设置函数
		{
			if( wid>0 || wid<20 )
			{
				width=wid;
			}
			else
			{
				cout << "Width out of range!!" << endl;
			}
		}
		float getLength()//Length的获取函数
		{
			return length;
		}
		float getWidth()//Width的获取函数
		{
			return width;
		}
		float perimerter()//计算周长
		{
			return 2.0*width+2.0*length;
		}
		float area()//计算面积
		{
			return width*length;
		}
};
int main()
{
	Rectangle rectangle;//创建一个Rectangle的对象
	float l;
	float w;
	cout << "Please enter the length and width of the rectangle :"<<endl;//提示输入长和宽
	cin >> l >> w ;
	rectangle.setLength( l );
	rectangle.setWidth( w );
	
	cout << "The perimerter of the rectangle is:"<< setw(4) << rectangle.perimerter() << endl << endl;
	cout << "The area of the rectangle is:" << setw(4) << rectangle.area() << endl << endl;
	
	return 0;
}

下面我们将创建一个比上面更复杂的Rectangle类。这个类只保存长方形四个角的笛卡儿坐标值。构造函数调用一个设置函数。该设置函数接受四组坐标值,验证它们都在第一象限中,没有一个x坐标或y坐标大于20.0,还验证提供的坐标确实构成长方形。该类提供成员函数计算length、width、perimeter 和area,其中长度是两维中的较大者。这个类还包含判定函数square,用以确定长方形是否是一个正方形。

代码实现如下:

//增强的Rectangle类.cpp 

#include <iostream>
#include <iomanip>
using namespace std;
class Rectangle
{
	private:
		float x1,x2,x3,x4;
		float y1,y2,y3,y4;
		int define; 
		
	public:
		Rectangle(float a,float b,float c,float d,float e,float f,float g,float h)//Initialize in constructor
		{
			setCoordinatesX(a,b,c,d);
			setCoordinatesY(e,f,g,h);
			
			if(x1==x3 && x2==x4 && y1==y2 && y3==y4)
			{
				cout << "this is a rectangle" << endl;
			}
			 
		}
		void setCoordinatesX(float X1,float X2,float X3,float X4 )
		{
			if(X1>=0&&X1<=20 && X2>=0&&X2<=20 && X3>=0&&X3<=20 && X4>=0&&X4<=20 )
			{
				x1=X1;
				x2=X2;
				x3=X3;
				x4=X4;
				
				define=1;	
			}
			else
			{
				cout << "x out of range" << endl;
				define=0;
			}
		
		} 
		
		void setCoordinatesY(float Y1,float Y2,float Y3,float Y4 )
		{
			if(Y1>=0&&Y1<=20 && Y2>=0&&Y2<=20 && Y3>=0&&Y3<=20 && Y4>=0&&Y4<=20 )
			{
				y1=Y1;
				y2=Y2;
				y3=Y3;
				y4=Y4;
				
				define=1;	
			}
			else
			{
				cout << "y out of range" << endl;
				define=0;
			}
				
		} 
		float length()
		{
			if( (x2-x1) < (y1-y3) )
			{
				cout << "Error: Length is less than width" << endl;
			}
			return x2-x1;
		}
		float width()
		{
			return y1-y3;
		}
		float perimeter()//计算周长 
		{
			return 2*(x2-x1)+2*(y1-y3);
		}
		float area()
		{
			return (x2-x1)*(y1-y3);
		}
		void square()//判定函数 
		{
			if( define==1 && (x1-x2)==(y1-y2) )
			{
				cout << "The rectangle is a square" <<endl; 
			}
			else
			{
				cout << "The rectangle is not a square" << endl;
			}
		}
		
};
int main()
{
	float x1,x2,x3,x4;
	float y1,y2,y3,y4;
	
	cout << "Please enter coordinates :" 
		 << endl;
	cin  >>x1 >>x2 >>x3 >>x4
		 >>y1 >>y2 >>y3 >>y4;
		
	Rectangle a(x1,x2,x3,x4,y1,y2,y3,y4);
	cout << "The length is : "    << a.length()    << endl;
	cout << "The width is : "     << a.width()     << endl;
	cout << "The perimeter is : " << a.perimeter() << endl;
	cout << "The area is : "      << a.area()      << endl;
	a.square();//判定是否为正方形
	return 0;
}

下面我们将继续增强这个Rectangle类,使它包含drawsetFillCharactersetPerimeterCharacter函数。draw成员函数在长方形所在第一象限的25x25封闭框中显示该长方形;setFillCharacter函数指定要绘制的长方形外部的字符,setPerimeterChar acter函数指定用来绘制长方形边缘的字符。这样我们就可以把长方形给绘制出来了。

 实现代码如下:

#include <iostream>
#include <string>

using namespace std;
class Rectangle
{
	private:
		float x1,x2,x3,x4;
		float y1,y2,y3,y4;
		int define;
		char outnumber,a;
		char innumber,b;
		float Length;
		float Width;
		
	public:
		Rectangle(float a,float b,float c,float d,float e,float f,float g,float h);//Initialize in constructor
		void setCoordinatesX(float X1,float X2,float X3,float X4 );
		void setCoordinatesY(float Y1,float Y2,float Y3,float Y4 );
		float length();
		float width();
		float perimeter();
		float area();
		void square();
		void draw();
		void setFillCharacter(char outnumber);
		void setPerimeterCharacter(char innumber);
		char getPerimeterCharacter();
		char getFillCharacter();
};

		Rectangle::Rectangle(float a,float b,float c,float d,float e,float f,float g,float h)//Initialize in constructor
		{
			setCoordinatesX(a,b,c,d);
			setCoordinatesY(e,f,g,h);
			define=0;
			outnumber='*';
			innumber='#';
			if(x1==x3 && x2==x4 && y1==y2 && y3==y4)
			{
				cout << "This is a rectangle !" << endl;
			}	 
		}
		
		void Rectangle:: setCoordinatesX(float X1,float X2,float X3,float X4 )
		{
			if(X1>=0&&X1<=20 && X2>=0&&X2<=20 && X3>=0&&X3<=20 && X4>=0&&X4<=20 )
			{
				x1=X1;
				x2=X2;
				x3=X3;
				x4=X4;
				define=1;	
			}
			else
			{
				cout << "x out of range" << endl;
				define=0;
			}
		
		}
		
		void Rectangle:: setCoordinatesY(float Y1,float Y2,float Y3,float Y4 )
		{
			if(Y1>=0&&Y1<=20 && Y2>=0&&Y2<=20 && Y3>=0&&Y3<=20 && Y4>=0&&Y4<=20 )
			{
				y1=Y1;
				y2=Y2;
				y3=Y3;
				y4=Y4;
				
				define=1;	
			}
			else
			{
				cout << "y out of range" << endl;
				define=0;
			}
				
		}
		
		float Rectangle:: length()
		{
			if( (x2-x1) < (y1-y3) )
			{
				cout << "Error: Length is less than width" << endl;
			}
			Length = x2-x1;
			return Length;
		}
		
		float Rectangle:: width()
		{
			Width =  y1-y3;
			return Width;
		}
		
		float Rectangle:: perimeter()//计算周长 
		{
			return 2*(x2-x1)+2*(y1-y3);
		}
		
		float Rectangle:: area()
		{
			return (x2-x1)*(y1-y3);
		}
		
		void Rectangle:: square()//判定函数 
		{
			if( define==1 && (x1-x2)==(y1-y2) )
			{
				cout << "The rectangle is a square" <<endl; 
			}
			else
			{
				cout << "The rectangle is not a square" << endl;
			}
		}
		
		void Rectangle::draw() //The rectangle is shown in a closed box of 25 * 25
		{
			int i,j;		
				for(i=25;i>=0;i--)	
				{	
					cout<<endl;	
					for(j=0;j<25;j++)	//2 9 6 9 2 1 6 1
					{	
						if(i<=y2 && i>=y3)	
						{	
							if(j==x1 || j==x2)
								cout<<getPerimeterCharacter();	
							else 
							{
								
								if(i==y1||i==y3)
								{
								
									if(j<x2&&j>x3)	
									{
										cout<<getPerimeterCharacter();
									}
										else 
										cout<<getFillCharacter();
								}	
								else 
								cout<<getFillCharacter();
							}
	
						}
	
						else cout<<getFillCharacter();
	
					}
	
				}
				cout<<endl;
			
		}
		
		void Rectangle::setFillCharacter(char b) //Draw the characters outside the rectangle
		{
			outnumber=a;;
		}
		
		void Rectangle::setPerimeterCharacter(char b) //Draw characters with rectangular edges
		{
			innumber=b;
		}
		
		char Rectangle::getFillCharacter()
		{
			return outnumber;
		}
		
		char Rectangle::getPerimeterCharacter()
		{
			return innumber;
		}
		
		
int main()
{
	float x1,x2,x3,x4;
	float y1,y2,y3,y4;
	
	cout << "Please enter coordinates :" //2 9 6 9 2 1 6 1 测试数据
		 << endl;
	cin  >>x1 >>y1 >>x2 >>y2
		 >>x3 >>y3 >>x4 >>y4;
		
	Rectangle a(x1,x2,x3,x4,y1,y2,y3,y4);
	cout << "The length is    : "      << a.length()    << endl;
	cout << "The width is     : "      << a.width()     << endl;
	cout << "The perimeter is : "      << a.perimeter() << endl;
	cout << "The area is      : "      << a.area()      << endl;
	a.square();
	cout << endl;
	a.setFillCharacter('#');
	a.setPerimeterCharacter('*');  
	a.draw();
	return 0;
} 

以上就是我对Rectangle类的设计,学习C++不久,能力有限,只能写到这里,若有不足,欢迎在评论区留言。

#include <iostream> #include <string> using namespace std; struct CPoint { int x ; int y ; }; class CRectangle { private: const int id;//常量数据成员 static int total;//静态数据成员 const static string sclass; const static int f=1.0f; CPoint lefttop ; CPoint rightdown ; public: CRectangle( ); CRectangle( CPoint& lt, CPoint& rd ); CPoint GetLefttop() const { return lefttop; } CPoint GetRightdown() const { return rightdown; } void SetLefttop(CPoint & pt) { lefttop=pt; } void SetRightdown(CPoint & pt) { rightdown=pt; } int Getid() const { return id; } static int Gettotal() { return total; } int Area( ) const; int Perimeter( ) const; }; int CRectangle::total=0;//静态数据成员必须在的外部定义(正好一次)。 const string CRectangle::sclass="CRectangle"; CRectangle::CRectangle( ):id(++total) { lefttop.x=0; lefttop.y=0; rightdown.x=1; rightdown.y=1; } CRectangle::CRectangle( CPoint& lt, CPoint& rd ):id(++total) { lefttop = lt ; rightdown = rd ; } int CRectangle::Area( ) const { int wd= rightdown.x - lefttop.x ; int ht= rightdown.y - lefttop.y ; return wd * ht ; } int CRectangle::Perimeter( ) const { int wd= rightdown.x - lefttop.x ; int ht= rightdown.y - lefttop.y ; return 2 * ( wd + ht ) ; } int main() { CPoint lt, rd; cin >> lt.x >> lt.y; cin >> rd.x >> rd.y; CRectangle crt(lt,rd);//调用有参构造函数 CRectangle crt2;//调用默认构造函数 //创建常量对象 const CRectangle crt3(lt,rd); cout<<"当前创建的矩形个数为:"; cout<<CRectangle::Gettotal()<<endl; //返回矩形的左上和右下点 CPoint lt1=crt.GetLefttop(); CPoint lt2=crt.GetRightdown(); //显示矩形的坐标 cout<<crt.Getid()<<"号矩形的坐标是:"<<"("<<lt1.x<<","<<lt1.y<<"), "; cout<<"("<<lt2.x<<","<<lt2.y<<")"<<endl; //显示矩形的面积周长 cout << "Area:"<<crt.Area( )<<endl; cout <<"Perimeter:"<<crt.Perimeter( )<<endl; //修改矩形的左上角点 cout<<"请输入矩形新的左上点坐标:"; cin>> lt.x>>lt.y; crt.SetLefttop(lt); lt1=crt.GetLefttop(); //显示修改后矩形的坐标 cout<<"矩形的坐标是:"<<"("<<lt1.x<<","<<lt1.y<<"), "; cout<<"("<<lt2.x<<","<<lt2.y<<")"<<endl; //显示修改后矩形的面积周长 cout << "Area:"<<crt.Area( )<<endl; cout <<"Perimeter:"<<crt.Perimeter( )<<endl; }
评论 31
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值