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

C++问题:设计一个名为Rectangle的矩形类,其属性为矩形的左下角和右上角两个点的坐标,能计算矩形的面积
利用组合类,能运行的话会追加分~
大胆1900 1年前 已收到1个回答 举报

zywbhk2 春芽

共回答了13个问题采纳率:100% 举报

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

1年前

3
可能相似的问题
Copyright © 2024 YULUCN.COM - 雨露学习互助 - 16 q. 0.032 s. - webmaster@yulucn.com