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

实现效果

相关推荐
jieyucx5 小时前
Go 语言进阶:构造函数、父子结构体与组合复用详解
服务器·算法·golang·继承·结构体·构造函数
澈2075 小时前
滑动窗口算法:双指针高效解题秘籍
数据结构·c++·算法
之歆5 小时前
DAY_10 JavaScript 深度解析:原型链 · 引用类型 · 内置对象 · 数组方法全攻略(下)
开发语言·前端·javascript·ecmascript
risc1234565 小时前
python 的字符串前缀
开发语言·python
小程故事多_805 小时前
Agent Loop 核心突破,上下文压缩四大流派,重新定义窗口资源利用率
java·开发语言·人工智能
咩咦5 小时前
C++学习笔记12:类和对象入门
c++·学习笔记·类和对象·封装·struct·class
渣渣苏5 小时前
硬核拆解 HNSW:亿级向量如何实现毫秒级召回?(上篇)
人工智能·算法·支持向量机·ai·向量数据库·hnsw·智能体
如竟没有火炬5 小时前
字符串相乘——int数组转字符串
开发语言·数据结构·python·算法·leetcode·深度优先
吃好睡好便好5 小时前
在Matlab中绘制三维等高线图
开发语言·python·学习·算法·matlab·信息可视化
天若有情6735 小时前
自制C++万能字符串流式库 formort.h|对标标准库endl,零拷贝链式拼接神器
开发语言·c++