-->
当前位置:首页 > 题库 > 正文内容

Luz4年前 (2021-03-08)题库1519
6-4 使用类计算矩形的面积 (10 分)

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

矩形类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
作者
李廷元
单位
民用航空飞行学院
代码长度限制
16 KB
时间限制
400 ms
内存限制
64 MB
int Rectangle::getArea(){
    return length*width;
}
void Rectangle::setWidth(int w){
    width=w;
}
void Rectangle::setLength(int l){
    length=l;
}