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

实现效果

相关推荐
hutaotaotao4 分钟前
c++中的输入输出流(标准IO,文件IO,字符串IO)
c++·io·fstream·sstream·iostream
机器学习之心6 分钟前
机器学习用于算法交易(Matlab实现)
算法·机器学习·matlab
江梦寻6 分钟前
MacOS下Homebrew国内镜像加速指南(2025最新国内镜像加速)
开发语言·后端·python·macos·架构·策略模式
AL流云。6 分钟前
【优选算法】C++滑动窗口
数据结构·c++·算法
霖檬ing9 分钟前
Python——MySQL远程控制
开发语言·python·mysql
qq_429879671 小时前
省略号和可变参数模板
开发语言·c++·算法
优秀的颜2 小时前
计算机基础知识(第五篇)
java·开发语言·分布式
CodeWithMe2 小时前
【C/C++】std::vector成员函数清单
开发语言·c++
uyeonashi2 小时前
【QT控件】输入类控件详解
开发语言·c++·qt
飞川撸码2 小时前
【LeetCode 热题100】网格路径类 DP 系列题:不同路径 & 最小路径和(力扣62 / 64 )(Go语言版)
算法·leetcode·golang·动态规划