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

实现效果

相关推荐
我是坏垠26 分钟前
Crypto、Cipher与Password:Java加密开发的三个核心概念
java·开发语言·python
white_ant1 小时前
JDK LTS 版本升级迁移注意事项大全
java·开发语言
Bobolink_1 小时前
跨境业务网络环境评估,“干净、一致、独享”三个关键指标
开发语言·网络·php·跨境网络·网络环境
你怎么知道我是队长1 小时前
JavaScript的变量和数据类型介绍
开发语言·javascript·ecmascript
xingke1 小时前
C 语言多文件编程:(一)头文件、extern、编译链接
c语言·开发语言·多文件
战族狼魂1 小时前
GPT-5.6与Grok 4.5重磅发布
人工智能·算法·大模型·大语言模型
白日焰火12 小时前
基于 OpenSpec 实现规范驱动开发
算法·交互
戮漠summer2 小时前
Missing Semester 计算机教育中缺失的一课 Lecture 01 Shell
开发语言·后端·scala
jinyishu_2 小时前
C++ string使用方法
开发语言·c++
imuliuliang2 小时前
关于图搜索算法的性能建模与可预测性研究7
算法