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

实现效果

相关推荐
fpcc8 分钟前
C++23中的模块应用说明之五综合应用和重点分
c++·c++23
云上凯歌9 分钟前
01 GB28181协议基础理解
java·开发语言
FakeOccupational21 分钟前
【电路笔记 PCB】Altium Designer : AD使用教程+Altium Designer常见AD操作命令与流程
开发语言·笔记
鹿角片ljp26 分钟前
力扣7.整数反转-从基础到边界条件
算法·leetcode·职场和发展
java修仙传29 分钟前
力扣hot100:前K个高频元素
算法·leetcode·职场和发展
毕设源码-钟学长35 分钟前
【开题答辩全过程】以 基于Java的运动器材销售网站为例,包含答辩的问题和答案
java·开发语言
Miketutu41 分钟前
Flutter学习 - 组件通信与网络请求Dio
开发语言·前端·javascript
嗷嗷哦润橘_1 小时前
从萝卜纸巾猫到桌游:“蒸蚌大开门”的设计平衡之旅
人工智能·算法·游戏·概率论·桌游
workflower1 小时前
软件需求规约的质量属性
java·开发语言·数据库·测试用例·需求分析·结对编程
xiaoye-duck1 小时前
吃透C++类和对象(下):内部类、匿名对象及编译器优化的深度解析
c++