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

实现效果

相关推荐
晓1313几秒前
JavaScript加强篇——第五章 DOM节点(加强)与BOM
java·开发语言·javascript
我是唐青枫17 分钟前
C#.NET serilog 详解
开发语言·c#·.net
云空27 分钟前
《探索电脑麦克风声音采集多窗口实时可视化技术》
人工智能·python·算法
小林C语言38 分钟前
11.9 C++对象指针
c++
沧澜sincerely41 分钟前
二分查找【各种题型+对应LeetCode习题练习】
算法·leetcode·二分查找
oioihoii1 小时前
C++11中的std::minmax与std::minmax_element:原理解析与实战
java·开发语言·c++
超龄超能程序猿1 小时前
使用 Python 对本地图片进行图像分类
开发语言·人工智能·python·机器学习·分类·数据挖掘·scipy
大千AI助手1 小时前
RLHF:人类反馈强化学习 | 对齐AI与人类价值观的核心引擎
人工智能·深度学习·算法·机器学习·强化学习·rlhf·人类反馈强化学习
wkj0011 小时前
php中调用对象的方法可以使用array($object, ‘methodName‘)?
android·开发语言·php
hudawei9961 小时前
kotlin中withContext,async,launch几种异步的区别
android·开发语言·kotlin