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

实现效果

相关推荐
武子康5 分钟前
Java-151 深入浅出 MongoDB 索引详解 性能优化:慢查询分析 索引调优 快速定位并解决慢查询
java·开发语言·数据库·sql·mongodb·性能优化·nosql
程序猿Eason22 分钟前
U587038 背包 题解
c++·算法·动态规划
爱吃芒果的蘑菇23 分钟前
C++之WebSocket初体验
网络·c++·websocket·网络协议
Tipriest_28 分钟前
C++ 中 using 的使用方法详解
c++·using
potato_may35 分钟前
第18讲:C语言内存函数
c语言·数据结构·算法
顾晨阳——1 小时前
C/C++字符串
c语言·c++·字符串
深耕AI1 小时前
【MFC实用技巧】对话框“边框”属性四大选项:None、Thin、Resizing、对话框外框,到底怎么选?
c++·mfc
ajassi20001 小时前
开源 C++ QT QML 开发(二十一)多媒体--视频播放
c++·qt·开源
患得患失9491 小时前
【Turborepo】【Next】 Turborepo + Next.js 前后端精简搭建笔记(带官网)
开发语言·javascript·笔记
拓端研究室1 小时前
视频讲解|Python遗传算法GA在车辆路径规划VRP数据优化中的应用
开发语言·人工智能·r语言