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

实现效果

相关推荐
白远山20 分钟前
家政服务派单平台搭建实战指南:从需求分析到系统设计全流程解析
java·开发语言·架构·uni-app·需求分析
午彦琳27 分钟前
2026.9.11
数据结构·python·算法
Dr_Fourier29 分钟前
mmap的使用与底层原理
linux·c++
Tairitsu_H36 分钟前
[C++] C++11右值引用与移动语义揭秘
开发语言·c++·c++11·右值引用·移动语义
(Charon)39 分钟前
【C++】 定时器进阶:多层时间轮原理与任务迁移
c++
蒸蒸yyyyzwd40 分钟前
cpp 选手备战秋招学习笔记 day32
c++·笔记·面试
麻辣布丁1 小时前
java集合篇面试模拟
java·开发语言·面试
niucloud-admin1 小时前
JAVA V6 多商户商城 开发文档——插件开发规范
java·开发语言
汉克老师1 小时前
GESP 四级明日冲刺:最后一晚只抓两个大方向
c++·gesp·小学生·学c++编程
302wanger1 小时前
蛋炒饭周刊 · 第 1 期(2026-09-07至2026-09-11)
算法