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

实现效果

相关推荐
Sand(ContextGate)2 分钟前
从开发者调试工具到消费级 App:一个 AI 电商导购项目的 C 端改造与 6 个连环踩坑实录
开发语言·人工智能·python·ai·langchain
_不会dp不改名_5 分钟前
leetcode_3727 最大平方交替和
linux·算法·leetcode
白杨尚青7 分钟前
C++入门篇(十七):priority_queue 完整模拟实现——仿函数、区间建堆与指针特化(附完整代码仓库)
开发语言·c++·stl·仿函数
信奥卷王11 分钟前
[GESP202509 六级] 货物运输(含视频)
算法
逐光者93321 分钟前
滤波器的 R 与 C 如何选择:从截止频率到工程实践
c语言·开发语言·r语言
Eider199826 分钟前
大模型输出格式幻觉解决思路
开发语言·前端·javascript
啦啦啦啦啦zzzz27 分钟前
分布式存储----缓存组
linux·服务器·c++·分布式·缓存
艾iYYY33 分钟前
思考: C++ 万能引用与完美转发
c++·windows·microsoft
余额瞒着我当琳35 分钟前
C++算法学习篇5--二分查找法
c++·学习·算法
Want59544 分钟前
Python太极八卦图
开发语言·python