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

实现效果

相关推荐
caimouse9 小时前
Reactos 第 9 章 设备驱动 — 9.4 内核劳务线程
开发语言·windows
syt_biancheng9 小时前
贪心算法(1)---简介
算法·贪心算法
Doker 多克9 小时前
Spring AI Alibaba—快速构建ReactAgent
java·开发语言·前端·ai编程
张忠琳10 小时前
【Go 1.26.4】Golang Slice 深度解析
开发语言·后端·golang
玖玥拾10 小时前
C/C++ 数据结构(四)链表与STL容器
c语言·数据结构·c++·链表·stl库
小白小宋10 小时前
【PUSCH番外篇】5G NR 相位补偿与频移校正:原理、流程与工程实现
算法·5g·matlab·信息与通信·信号处理
不吃土豆的马铃薯10 小时前
C++ 正则表达式入门详解
linux·服务器·网络·数据库·c++·正则表达式
满怀冰雪10 小时前
第15篇-链表基础-反转链表-合并链表与快慢指针
java·算法·链表
2zcode10 小时前
基于MATLAB语音信号变声算法设计与实现
算法·matlab·语音识别·变声算法
玖玥拾10 小时前
C/C++ 数据结构(一)基础概念、线性表链表
c语言·数据结构·c++·链表