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

实现效果

相关推荐
动词ing2 分钟前
【题目练习】算法 组合总和
算法
程序员-Benothing8 分钟前
Linux文本排序统计命令:sort、uniq、wc、cut、tr实战
java·开发语言·算法
CRMEB9 分钟前
前后端技术栈全面换代!CRMEB 多商户(Java)v3.0更新预告
java·开发语言
开发者联盟league11 分钟前
c++11 使用chrono库转换时间点和字符串,计算时间点间隔
java·c++·算法
JQLvopkk19 分钟前
HslCommunication,一个工具测遍所有 PLC
开发语言·人工智能·c#·交互
阿狗童鞋26 分钟前
Python爬虫进阶实战指南
开发语言·爬虫·python
下页、再停留32 分钟前
【C#桌面客户端系列学习-7】数据查询展示
开发语言·c#·visual studio
皓月盈江43 分钟前
Go读写TXT文件
开发语言·后端·golang·go·读写txt文件·bufio·go读写txt
老歌老听老掉牙44 分钟前
两个平面旋转平移坐标系间的坐标变换关系
python·算法·平面·旋转·平移
qq_401700411 小时前
Qt Signal Mapper:一堆按钮,怎么共用一个槽函数
开发语言·qt