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;
}

实现效果

相关推荐
渊鱼L7 分钟前
CAD随机颗粒插件2D V2.0 更新说明
算法
Lazionr9 分钟前
unordered_map和unordered_set的使用
开发语言·c++·算法
行者全栈架构师14 分钟前
从 55% 到 6%:一个快餐营养规划器的算法迭代实录
后端·算法·架构
进化中的刀盾兽24 分钟前
从零实现 C++ Json-Rpc(九):从 TCP 字节流中拆出完整消息——MuduoBuffer 与 LVProtocol
c++·rpc
Xiu Yan31 分钟前
Python 数据分析:数据分析步骤
开发语言·python·数据分析
Laurie三省32 分钟前
现场版却配上录音室版的歌词?聊聊我给开源 Mac 歌词软件写的多源打分算法
算法·macos·go
黑妹天下第一乖40 分钟前
小智改造实战解读-用kokoro-onnx替换云端 TTS 的完整记录
大数据·开发语言·人工智能·python·交互
郑州光合科技余经理40 分钟前
本地生活平台搭建:跨业态用户标识怎么贯通
java·开发语言·前端·后端·uni-app·php·ai编程
对空六课42 分钟前
Vue 组件曝光埋点为什么会重复触发?去重方案与实现思路
服务器·前端·算法·数据分析
HZY1618yzh1 小时前
下一代编程语言出炉
c++·算法