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

实现效果

相关推荐
普通人64 小时前
php处理百度编辑器有误html
开发语言·编辑器·html·php·1024程序员节
Tisfy4 小时前
LeetCode 3723.数位平方和的最大值:构造
数学·算法·leetcode·字符串·题解
hetao17338374 小时前
联盟场-提高组-R4 & ZYZSC-S-Round 7
c++·算法
沉下心来学鲁班4 小时前
具身智能入门:用 Python 让 MicroDuck 机器狗学会走路
开发语言·人工智能·python·机器人
geovindu4 小时前
rust: Chain of Responsibility Pattern (续)
开发语言·后端·设计模式·rust·责任链模式·行为模式
诚心呈意共享外卖配送系统4 小时前
多平台订单聚合系统的技术架构:接 API 只是开始
java·开发语言·架构·系统架构·创业创新
碧海银沙音频科技研究院4 小时前
基于炬芯ATS3089X音箱的音乐播放歌词显示功能实现
人工智能·驱动开发·嵌入式硬件·算法
学逆向的4 小时前
C 浮点数e(科学计数法)
c语言·开发语言·网络安全
知识分享小能手4 小时前
C学习教程,从入门到精通,C语言数组 —— 知识点详解(8)
c语言·学习·算法
hetao17338375 小时前
联盟场-提高组-R2 & ZYZSC-S-Round 5
c++·算法