1个回答
展开全部
给你两种 写法
第一种:
#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);
}
第一种:
#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);
}
本回答被提问者和网友采纳
已赞过
已踩过<
评论
收起
你对这个回答的评价是?
推荐律师服务:
若未解决您的问题,请您详细描述您的问题,通过百度律临进行免费专业咨询