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

实现效果

相关推荐
sharemylove6 小时前
Java集合框架全面详解
java·开发语言·经验分享·笔记·学习
IvanCodes9 小时前
模型如何自我改进?拆解 RSI 的模型权重与 Harness 两条路线
人工智能·算法·ai·论文·agent·rsi
罗西的思考10 小时前
【轻量化复刻大模型 / KIMI K3】MXFP4 / MXFP8 量化感知训练(QAT)& 演示
人工智能·算法·机器学习
郝学胜-神的一滴11 小时前
Effective Python 条款43:自定义容器类
开发语言·windows·python·pandas·scikit-learn·pyqt
好焊孙辉12 小时前
一条产线省下 30 个电机位:Z 轴和 R 轴,该合成一个了
开发语言·r语言
布莱克60512 小时前
Swift 中的逃逸闭包与非逃逸闭包:@escaping 到底在做什么
开发语言·ios·swift·闭包·逃逸
郝学胜-神的一滴12 小时前
Effective STL 条款 14:用扩容函数避免重新分配
linux·开发语言·网络·数据结构·c++·单片机
旖旎夜光13 小时前
LeetCode 493: 翻转对 (归并排序) —— 题解
c++·笔记·学习·算法·leetcode·排序算法
吃我一窝jio13 小时前
Java IO流核心分类与应用
java·开发语言·笔记
阿enna13 小时前
检测指标入门:IoU、Precision、Recall与mAP
人工智能·深度学习·算法·yolo·目标检测·计算机视觉·cnn