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

实现效果

相关推荐
智者知已应修善业2 分钟前
【PAT乙级真题解惑1012数字分类】2025-3-29
c语言·c++·经验分享·笔记·算法
每天要多喝水29 分钟前
动态规划Day30:买卖股票
算法·动态规划
v_for_van35 分钟前
力扣刷题记录6(无算法背景,纯C语言)
c语言·算法·leetcode
之歆36 分钟前
HA 高可用集群指南
java·开发语言
-To be number.wan39 分钟前
算法学习日记 | 双指针
c++·学习·算法
lsx2024061 小时前
电子商务网站主机:选择与维护指南
开发语言
样例过了就是过了1 小时前
LeetCode热题100 最大子数组和
数据结构·算法·leetcode
wangluoqi1 小时前
c++ 逆元 小总结
开发语言·c++
BackCatK Chen1 小时前
第十五章 吃透C语言结构与数据形式:struct/union/typedef全解析
c语言·开发语言·数据结构·typedef·结构体·函数指针·联合体
瓦特what?1 小时前
插 入 排 序
开发语言·c++