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

实现效果

相关推荐
小字节,大梦想32 分钟前
【数据结构】详细介绍各种排序算法,包含希尔排序,堆排序,快排,归并,计数排序
c语言·数据结构·c++·算法
程序猿进阶33 分钟前
ThreadLocal 释放的方式有哪些
java·开发语言·性能优化·架构·线程池·并发编程·threadlocal
程序者王大川38 分钟前
【大数据】如何读取多个Excel文件并计算列数据的最大求和值
开发语言·python·excel·big data
Mryan200540 分钟前
OpenJudge | 寻找中位数
开发语言·数据结构·c++·算法·openjudge
lizi888882 小时前
打包Python代码的常用方法实现程序exe应用
开发语言·python
api茶飘香3 小时前
守护应用边界:通过反射API实现安全的输入输出过滤
java·开发语言·python·安全·django·virtualenv·pygame
杀死一只知更鸟debug3 小时前
策略模式的小记
java·开发语言·策略模式
七十二五3 小时前
matlab数据批量保存为excel,文件名,行和列的名称设置
经验分享·算法·matlab·青少年编程·矩阵·excel
efls1113 小时前
Qt_了解Qt Creator
开发语言·qt
请揣满RMB3 小时前
Qt常用控件——QRadioButton和QCheckBox
开发语言·c++·qt