定义并实现一个矩形类,有长和宽两个属性,由成员函数计算矩形的面积。

矩形类Rectang接口定义如下:

class Rectangle {
public:
    void setLength(int l);//设置矩形的长度
    void setWidth(int w); //设置矩形的宽度
    int getArea();    //计算并返回矩形的面积
private:
    int length, width;  //矩形的长度和宽度				
};

请实现Rectangle类的成员函数。

裁判测试程序样例:

#include <iostream>
using namespace std;

class Rectangle {
public:
    void setLength(int l);//设置矩形的长度
    void setWidth(int w); //设置矩形的宽度
    int getArea();        //计算并返回矩形的面积
private:
    int length, width;    //矩形的长度和宽度				
};

int main()
{
    Rectangle r;
    int len, w;
    cin >> len >> w;
    r.setLength(len);
    r.setWidth(w);
    cout << r.getArea() << "\n";

    return 0;
}

/* 你的代码将嵌在这里 */

输入样例:

10 20
200
----------------------------------------------------------------------------
-----------------------------------------------------------------------------
            参考代码
-----------------------------------------------------------------------------
如有错误,感谢指出!


#include <iostream>
using namespace std;

class Rectangle {
public:
    void setLength(int l);//设置矩形的长度
    void setWidth(int w); //设置矩形的宽度
    int getArea();        //计算并返回矩形的面积
private:
    int length, width;    //矩形的长度和宽度                
};

int main()
{
    Rectangle r;
    int len, w;
    cin >> len >> w;
    r.setLength(len);
    r.setWidth(w);
    cout << r.getArea() << "\n";

    return 0;
}

/* 请在这里填写答案 */
void Rectangle::setLength(int l){length=l;}
void Rectangle::setWidth(int w){width=w;}
int Rectangle::getArea()
{
    return length*width;
}

 

欢迎指教,一起学习!

未经本人允许,请勿转载!

谢谢!



相关文章:

  • 2022-12-23
  • 2021-08-04
  • 2022-12-23
  • 2021-12-12
  • 2021-08-20
  • 2022-12-23
  • 2022-12-23
  • 2022-12-23
猜你喜欢
  • 2022-01-26
  • 2022-12-23
  • 2022-12-23
  • 2021-08-25
  • 2021-10-09
  • 2021-09-15
相关资源
相似解决方案