8.06 C++作业

使用类定义实现隔离储存计算

1.头文件

cpp 复制代码
#ifndef CLASS_H
#define CLASS_H
#include <iostream>
using namespace std;

class rect
{
private:
    int width;
    int height;
public:
    void init(int width,int height);
    void show();
    void setw(int w);
    void seth(int h);
};

#endif // CLASS_H

2.功能函数

cpp 复制代码
#include "class.h"
void rect::init(int width,int height)
{
    this->width=width;
    this->height=height;
}
void rect::show()
{
    cout << "面积为:" << width*height << endl;
    cout << "周长为:" << 2*(width+height) << endl;
}
void rect::setw(int w)
{
    width=w;
}
void rect::seth(int h)
{
    height = h;
}

3.主函数

cpp 复制代码
#include "class.h"

int main()
{
    rect s1;
    cout << "Hello World!" << endl;
    int a,b;
    cout << "请输入矩形的宽和长:";
    cin >> a >> b;
    s1.init(a,b);
    s1.show();
    cout << "请修改矩形的宽度:" ;
    cin >> a ;
    s1.setw(a);
    cout << "请修改矩形的长度:" ;
    cin >> b;
    s1.seth(b);
    s1.show();
    return 0;
}

实现效果

相关推荐
Highcharts.js5 小时前
BI可视化图表“维护难”问题的解决方案
开发语言·前端·javascript·编辑器·ecmascript·bi·highcharts
碧海银沙音频科技研究院5 小时前
实现炬芯ATS308X的usb cdc 串口IAP 固件升级功能实现
人工智能·嵌入式硬件·深度学习·算法
云淡风轻~窗明几净5 小时前
角谷猜想与百度AI的对话dialog(原始稿)
人工智能·算法·机器学习·图论
yixun_gdas6 小时前
政企网站错敏巡查:错敏都藏在哪些位置?
开发语言·前端·javascript·内容运营
小羊没烦恼!6 小时前
.NET 11 Runtime Async 详解
java·运维·开发语言·javascript·python
Дерек的学习记录6 小时前
Unreal Engine 5:开关门实现和流程控制
开发语言·学习·ue5
kyle~6 小时前
时间同步 --- PTP 与 Scheduled Action Command
c++·数码相机·机器人·传感器
小O的算法实验室6 小时前
ASOC,分层分数阶粒子群算法+无人机电场路径规划
算法
重生之小比特7 小时前
【算法拾光】二分查找|搜索插入位置 + x 的平方根
算法