10.8c++作业

cpp 复制代码
#include <iostream>

using namespace std;
class Rect
{
    int width;  //宽
    int height; //高
public:
    //初始化函数
    void init(int w,int h)
    {
        width=w;
        height=h;
    }
    //更改宽度
    void set_w(int w)
    {
        width=w;
    }
    //更改高度
    void set_h(int h)
    {
        height=h;
    }
    //输出矩形周长和面积
    void show()
    {
        cout << "宽: " << width << endl;
        cout << "高: " << height << endl;
        cout << "周长: " << (width+height)*2 << endl;
        cout << "面积: " << (width*height) << endl;

    }
};

int main()
{
    Rect r1;

    int a,b;
    cout << "请输入宽:" << endl;
    cin >> a;
    cout << "请输入高:" << endl;
    cin >> b;
    r1.init(a,b);
    r1.set_w(a);
    r1.set_h(b);
    r1.show();
    return 0;
}
相关推荐
Kx_Triumphs2 小时前
HDU4348 To the moon(主席树区间修改模板)
算法·题解
旖-旎2 小时前
《LeetCode647 回文子串 || LeetCode 5 最长回文子串》
c++·算法·leetcode·动态规划·哈希算法
玖玥拾2 小时前
C# 语言进阶(十五)C# 游戏服务端 MySQL 数据库
服务器·开发语言·网络·数据库·mysql·c#
铅笔侠_小龙虾3 小时前
Rust 学习目录
开发语言·学习·rust
Darkwanderor3 小时前
对Linux的进程控制的研究
linux·运维·c++
云泽8083 小时前
从零吃透 C++ 异常:抛出捕获、栈展开、异常重抛与编码规范详解
开发语言·c++·代码规范
轻颂呀3 小时前
约瑟夫环问题
算法
灯澜忆梦3 小时前
GO---可见性规则
开发语言·golang
REDcker3 小时前
libdatachannel 快速入门
c++·webrtc·datachannel