【类和对象的创建和使用】第2关:设计一个长方形类

设计 Rectangle 类和实现两个普通函数,类中有两个成员变量和两个函数,其中成员变量的访问性为私有,函数的访问性为公有,具体要求如下:

高度:int height

宽度:int width

设置长方形的高和宽函数:void Set(int h, int w)

获取长方形的面积函数:int GetArea()

Rectangle GetRect(int h,int w)普通函数,h 、w 分别代表长方形的高宽,函数用来创建一个 Rectangle 对象并返回。

int GetRectArea(Rectangle rect)普通函数,函数用来获取 rect 对象的面积。
class Rectangle
{
public:
    int height;
    int width;

    void Set(int h, int w);
    int GetArea();
};

void Rectangle::Set(int h, int w)
{
    this->height = h;
    this->width = w;
}

int Rectangle::GetArea()
{
    return this->height * this->width;
}

Rectangle GetRect(int h,int w)
{
    Rectangle r;
    r.height = h;
    r.width = w;

    return r;
}

int GetRectArea(Rectangle rect)
{
    return rect.GetArea();
}


  • 4
    点赞
  • 4
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值