中易网

设计并测试一个名为Rectangle的矩形类,其属性为矩形的左下角与右上角两个点的坐标,能计算矩形的面积

答案:2  悬赏:60  
解决时间 2021-04-28 07:07
  • 提问者网友:失败的占卜者
  • 2021-04-28 02:11
解:
源程序:
#include
class Rectangle
{
public:
Rectangle (int top, int left, int bottom,int right);
~Rectangle () {}

int GetTop() const { return itsTop; }
int GetLeft() const { return itsLeft; }
int GetBottom() const { return itsBottom; }
int GetRight() const { return itsRight; }

void SetTop(int top) { itsTop = top; }
void SetLeft (int left) { itsLeft = left; }
void SetBottom (int bottom) { itsBottom =bottom; }
void SetRight (int right) { itsRight =right; }

int GetArea() const;

private:
int itsTop;
int itsLeft;
int itsBottom;
int itsRight;
};
Rectangle::Rectangle(int top, int left, intbottom, int right)
{
itsTop = top;
itsLeft = left;
itsBottom = bottom;
itsRight = right;
}

int Rectangle::GetArea() const
{
int Width = itsRight-itsLeft;
int Height = itsTop - itsBottom;
return (Width * Height);
}

int main()
{
RectangleMyRectangle (100, 20, 50, 80 );

int Area = MyRectangle.GetArea();

cout << "Area: " <return 0;
}
程序运行输出:
Area: 3000
Upper Left X Coordinate: 20
有几点疑惑,求大神解释:
1.~Rectangle () {}这个析构函数具体有什么用?
2.int GetTop() const { return itsTop; } const有什么含义
3.
void SetTop(int top) { itsTop = top; }
void SetLeft (int left) { itsLeft = left; }
void SetBottom (int bottom) { itsBottom =bottom; }
void SetRight (int right) { itsRight =right; }
itsTop = top itsLeft = left itsBottom =bottom 是什么意思

4.还有一种表达方式是this->top=top 这里的this表示什么 有什么用法?
最佳答案
  • 二级知识专家网友:心与口不同
  • 2021-04-28 03:07
1.析构函数往往用来做“清理善后” 的工作,即释放class Rectangle所占用的内存。
2.const表示函数不能修改对象数据成员。
3.itsTop = top 表示:把传进来的参数top的值赋给类 Rectangle的数据成员itsTop

itsLeft = left 表示:把传进来的参数left的值赋给类 Rectangle的数据成员itsLeft
itsBottom =bottom 表示:把传进来的参数bottom 的值赋给类 Rectangle的数据成员itsBottom
4.每个类都有自己一个指向自己的指针,这个指针就是this。this->top就是代表类Rectangle中的top ,而top则指类外部传进来的参数。
全部回答
  • 1楼网友:余生继续浪
  • 2021-04-28 03:53
给你两种 写法
第一种:
#include <iostream.h>
#include <math.h>

class rectangle
{
public:
 rectangle(int left,int bottom,int right,int top);
 ~rectangle(){ }
 //int getleft() {return itsleft;}
 int getbottom() {return itsbottom;}
 int getright() {return itsright;}
 int gettop() { return itstop;}

 void setleft(int left) {itsleft=left;}
 void setbottom(int bottom) {itsbottom=bottom;}
 void setright(int right) {itsright=right;}
 void settop(int top) {itstop=top;}

 int getarea();
private:
 int itsleft;
 int itsbottom;
 int itsright;
 int itstop;
int width;
int height;
};
rectangle::rectangle(int left,int bottom,int right,int top)
{
 itsleft=left;
 itsbottom=bottom;
 itsright=right;
 itstop=top;
}

int rectangle::getarea()
{
 width=abs(itsright-itsleft);
 height=abs(itstop-itsbottom);
 cout<<"该矩形的长:"<<width<<endl;
 cout<<"该矩形的宽:"<<height<<endl;
 return (width*height);
}

void main()
{
 rectangle myrectangle(20,20,60,50);
 int area;
 area=myrectangle.getarea();
 cout<<"area:"<<area<<endl;
 myrectangle.setleft(10);
 area=myrectangle.getarea();
 cout<<"area:"<<area<<endl;
}

第二种 :
#include<iostream.h>
#include<math.h>
class rectangle
{
private:
 int left;
 int bottom;
 int right;
 int top;
public:
 void setpoint(int l,int b,int r,int t)
 {
 left=l;
 bottom=b;
 right=r;
 top=t;
 }
 void cal(int left,int bottom,int right,int top)
 {
 int s;
 s=abs(left-right)*abs(bottom-top);
 cout<<"该长方形的面积为:"<<s<<endl;
 }
 void getp1(int left,int bottom)
 {
 cout<<"长方形的左下角坐标是:"<<left<<","<<bottom<<endl;
 }
 void getp2(int right,int top)
 {
 cout<<"长方形的右上角坐标是:"<<right<<","<<top<<endl;
 }
};

void main()
{ 
 rectangle r1;
 int l,b,r,t;
 cout<<"请输入长方形的左下角的坐标:";
 cin>>l>>b;
 cout<<"请输入长方形的右上角的坐标:";
 cin>>r>>t;
 r1.getp1(l,b);
 r1.getp2(r,t);
 r1.cal(l,b,r,t);
}
我要举报
如以上回答内容为低俗、色情、不良、暴力、侵权、涉及违法等信息,可以点下面链接进行举报!
点此我要举报以上问答信息!
大家都在看
推荐信息