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

实现效果

相关推荐
River4161 小时前
Javer 学 c++(十三):引用篇
c++·后端
感哥4 小时前
C++ std::set
c++
Fanxt_Ja4 小时前
【LeetCode】算法详解#15 ---环形链表II
数据结构·算法·leetcode·链表
侃侃_天下4 小时前
最终的信号类
开发语言·c++·算法
茉莉玫瑰花茶4 小时前
算法 --- 字符串
算法
博笙困了5 小时前
AcWing学习——差分
c++·算法
NAGNIP5 小时前
认识 Unsloth 框架:大模型高效微调的利器
算法
NAGNIP5 小时前
大模型微调框架之LLaMA Factory
算法
echoarts5 小时前
Rayon Rust中的数据并行库入门教程
开发语言·其他·算法·rust
Python技术极客5 小时前
一款超好用的 Python 交互式可视化工具,强烈推荐~
算法