C++问题:设计一个名为Rectangle的矩形类,其属性为矩形的左下角和右上角两个点的坐标,能计算矩形的面积

利用组合类,能运行的话会追加分~

第1个回答  推荐于2018-04-17
给你两种 写法
第一种:
#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);
}本回答被提问者和网友采纳

C++问题:设计一个名为Rectangle的矩形类,其属性为矩形的左下角和右...
cout<<"长方形的左下角坐标是:"<<left<<","<<bottom<<endl;} void Getp2(int right,int top)

设计并测试一个名为Rectangle的矩形类,其属性为矩形的左下角和右上角...
{ Rectangle r(1.0,1.5,2.0,3.3);cout<<"矩形面积是:"<<r.Area()<<endl;}

...矩形类Rectangle,数据成员为矩形的左下角与右上角的坐标,利用成员函 ...
\/\/矩形类 class Rectangle { public:double x,y;\/\/左下 double _x,_y;\/\/右上 Rectangle(double x,double y,double _x,double _y){ this->x = x;this->y = y;this->_x = _x;this->_y = _y;} void p(){ cout<<"周长:"<<2*fabs(x-_x) + 2*fabs(y-_y)<<endl;} ...

c++ 定义一个矩形类CRectangle,矩形的左上角(Left,Top)与右下角坐标...
void calrect(CRectangle rec);void caltri(CTriangle tri);class CRectangle { public:CRectangle(int t,int l,int b,int r):top(t),left(l),bot(b),right(r){} friend void calrect(CRectangle rec);protected:int top,left;int bot,right;};class CTriangle { public:CTriangle(int ...

C++定义并实现一个矩形类
{ public:Rectangle(){ x1=0;x2=0;y1=0;y2=0;} Rectangle(double a1,double b1,double a2,double b2){ x1=a1;y1=b1;x2=a2;y2=b2;} void input(){ cout<<"请输入矩形左下角和右上角顶点的坐标:"<<endl;cin>>x1>>y1>>x2>>y2;} void output(){ cout<<"该矩形的面积为:...

采用c++定义并实现一个矩形(Rectangle)类
代码如下:include <iostream>#include <string>using namespace std;class Rectangle {public:Rectangle() : length(length), width(width), color("") {}int GetLength() {return this->length;}void SetLength(int length) {this->length = length;}int GetWidth() {return this->width;}void...

用c++编写一个矩形类
<iostream>using namespace std;class Rectangle { private: double length; double height; double area; double circumference; public: Rectangle() {} Rectangle(double l, double h) { length = l; height=h; } Rectangle(const Rectangle &r) { length = r...

用C++设计一个矩形类
参考一下CRect的定义 class CRect : public tagRECT { public:\/\/ Constructors \/\/ uninitialized rectangle CRect();\/\/ from left, top, right, and bottom CRect(int l, int t, int r, int b);\/\/ copy constructor CRect(const RECT& srcRect);\/\/ from a pointer to another rect CRect(...

C++定义描述矩形的类Rectangle,其数据成员为矩形的中心坐标(X,Y...
class point{float x;float y;public:point(float px=0,float py=0) :x(px), y(py){}};class Rectangle:public point{public:Rectangle(){};Rectangle(float px, float py, float pl, float pw) :point(px, py), Length(pl), Width(pw){};float Area(){return Length*Width;};~...

c++如何设计一个矩形类(rect)?
{ public:rect(int width, int length) : width(width),length(length){} int Area(){ return length * width;} int Perimeter(){ return 2 * (length + width);} friend int add(rect &r1, rect &r2);private:int length;int width;};int add(rect &r1,rect &r2){ return r1....

相似回答