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

来源:百度知道 编辑:UC知道 时间:2024/04/28 16:47:51
利用组合类,能运行的话会追加分~

给你两种 写法
第一种:
#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);