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

实现效果

相关推荐
8Qi81 分钟前
LeetCode 746:使用最小花费爬楼梯 —— 题解笔记
java·笔记·算法·leetcode·动态规划
pipo5 分钟前
没雷达也能调 Nav2?我开源了一套仿真到实机复用的 ROS 2 3D LiDAR 导航工作空间
算法
曾几何时`7 分钟前
Go(四)Channel
开发语言·后端·golang
计算机安禾11 分钟前
【算法分析与设计】第44篇:随机化复杂度类:RP、BPP与去随机化猜想
java·数据结构·数据库·算法·机器学习
未若君雅裁14 分钟前
Java 线程基础:进程、线程、并发并行、创建方式与生命周期
java·开发语言
sugar__salt17 分钟前
JS正则表达式与字符串高阶实战精讲
开发语言·javascript·正则表达式
QT-Neal19 分钟前
C/C++ 程序段的概念与分类
c语言·c++
计算机安禾20 分钟前
【算法分析与设计】第45篇:交互式证明系统与零知识证明
算法·区块链·零知识证明
AI浩21 分钟前
梯度累积与 Micro-Batch 设计分层式精讲:有效批次、显存边界与分布式同步
开发语言·分布式·batch
自进化Agent智能体24 分钟前
Hermes架构全景图:从入口到交付的完整数据流
算法