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

实现效果

相关推荐
害人终害己8 分钟前
Redisson 连接 Redis 失败 报错解决方案
java·开发语言
何老师的GESP C++进阶课堂8 分钟前
1、数组知识点总结
c++·青少年编程·gesp c++
jimy118 分钟前
c++的静态类型和动态类型(二)
开发语言·c++
吠品18 分钟前
箭头函数为什么不能 new
开发语言·javascript·原型模式
stolentime23 分钟前
AT_arc182_a [ARC182A] Chmax Rush!题解
数据结构·c++·算法·动态规划
longlongzihan23 分钟前
LeetCode 189. 轮转数组:从直观模拟到最优原地算法
c++·算法·leetcode
谢亮_vipxieliang27 分钟前
PHP 8 新特性:构造器属性提升与实战
开发语言·后端·php
爱喝热水的呀哈喽29 分钟前
cpp二分库函数 lambda捕获
算法
鬼手点金36 分钟前
opencode-crawl4ai使用建议
java·开发语言·人工智能·python·机器学习
longlongzihan39 分钟前
LeetCode 76. 最小覆盖子串:滑动窗口算法详解
算法·leetcode·滑动窗口