我们先创建具有属性length(长度)和width(宽度)的类Rectangle(长方形),这两个属性的默认值为1。分别提供计算长方形perimeter(周长)和area(面积)的成员函数。另外,为length和width两个属性提供设置和获取函数。设置函数应该验证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类,使它包含draw、setFillCharacter和setPerimeterCharacter函数。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++不久,能力有限,只能写到这里,若有不足,欢迎在评论区留言。