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

实现效果

相关推荐
2401_894915531 分钟前
Geo优化系统源码部署搭建技术——PHP程序开发部署指南
开发语言·php
找磁材10 分钟前
空心杯电机与永磁体
开发语言·磁性材料·钕铁硼·磁钢
核数聚19 分钟前
【赛迪专访核数聚】深耕数据治理,打通数据孤岛夯实 AI 发展根基
大数据·人工智能·算法
我不管我就要叫小猪20 分钟前
C/C++----命名空间
c语言·开发语言·c++
小许同学记录成长37 分钟前
相对辐射定标技术文档
图像处理·算法
兮动人37 分钟前
Python字符串类型
开发语言·python·python字符串类型
自由随风飘1 小时前
JAVA中的面向对象-3
java·开发语言
白白白小纯1 小时前
算法篇—返回倒数第k个节点
c语言·数据结构·算法·leetcode
小羊先生car1 小时前
RTOS-F429-HAL-(动/静态)任务的创建(2026/7/27)
开发语言·算法·c#
无忧.芙桃1 小时前
数据结构之堆
c语言·数据结构·c++·算法·