随笔 - 400  文章 - 0  评论 - 6  阅读 - 3230

5月17日打卡

习题4-11

题目描述:

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

设计思路:

1.定义一个矩形类,包括长宽两个私有类型数据和构造函数,以及共有成员函数用于计算矩形面积。、

代码部分:

复制代码
#include<iostream>
using namespace std;
class Rectangle {
private:
    int length;
    int weight;
public:
    Rectangle(int a, int b)
    {
        length = a;
        weight = b;
    }
    int Area()
    {
        return length * weight;
    }
};
int main()
{
    int a, b;
    cin >> a >> b;
    Rectangle x(a, b);
    cout << x.Area() << endl;
}
复制代码

4-13定义一个Circle类,有数据成员radius(半径),成员函数getArea()计算圆的面积。

代码部分:

复制代码
#include<iostream>
using namespace std;
class Rectangle {
private:
    int radius;
public:
    Rectangle(int a)
    {
        radius = a;
    }
    double getArea()
    {
        return 3.1415926 * radius * radius;
    }
};
int main()
{
    int a;
    cin >> a;
    Rectangle x(a);
    cout << x.getArea() << endl;
}
复制代码

 

posted on   石铁生  阅读(34)  评论(0编辑  收藏  举报
相关博文:
阅读排行:
· DeepSeek 开源周回顾「GitHub 热点速览」
· 物流快递公司核心技术能力-地址解析分单基础技术分享
· .NET 10首个预览版发布:重大改进与新特性概览!
· AI与.NET技术实操系列(二):开始使用ML.NET
· 单线程的Redis速度为什么快?
< 2025年3月 >
23 24 25 26 27 28 1
2 3 4 5 6 7 8
9 10 11 12 13 14 15
16 17 18 19 20 21 22
23 24 25 26 27 28 29
30 31 1 2 3 4 5

点击右上角即可分享
微信分享提示