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

实现效果

相关推荐
Ws_1 分钟前
WPF 面试题 + 参考答案,偏 C# 桌面端开发高频。
开发语言·c#·wpf
程序猿编码2 分钟前
如何把远程文件变化“骗“成本地inotify事件:一个LD_PRELOAD钩子
c语言·开发语言·网络·tcp/ip·安全
lDevinl2 分钟前
【无标题】
数据结构·c++·青少年编程
菜菜的顾清寒5 分钟前
力扣HOT100(46)将有序数组转换为二叉搜索树
算法·leetcode·职场和发展
zincsweet6 分钟前
System V 共享内存:原理剖析、代码架构分析与双端通信实战
linux·c++
随意起个昵称8 小时前
区间dp-基础题目1(石子合并)
算法·动态规划
吞下星星的少年·-·8 小时前
线段树模板
算法
星空椰9 小时前
Python 面向对象高级:继承与类定义详解
开发语言·python
wunaiqiezixin9 小时前
如何在C++中创建和管理线程
c++
段一凡-华北理工大学9 小时前
2026 高炉炼铁智能化技术全景与演进路径~系列文章11:演进路径与行业未来
大数据·网络·人工智能·算法·工业智能体·高炉炼铁智能化