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

实现效果

相关推荐
rhythm-ring7 分钟前
宏定义续行符 \ 的使用与踩坑
c语言·c++
AI情绪识别开源30 分钟前
检信 AI 智能推广平台(代号:JX-Promote)
人工智能·算法·erlang
XZ-07000130 分钟前
week2-1-可视化
开发语言·python
Java后端的Ai之路38 分钟前
14、Python - 责任链模式
服务器·开发语言·人工智能·python·责任链模式
吴声子夜歌1 小时前
Java——基本类型
java·开发语言
老当益壮梁奶奶1 小时前
Linux软件编程学习笔记(八):进程间通信详解(1)
linux·c语言·笔记·学习·算法
江安下小雨1 小时前
muduo网络库(十六):新增连接池模块
网络·c++
「QT(C++)开发工程师」1 小时前
C++11 std::unique_ptr — 独占所有权的智能指针
c语言·开发语言·c++·qt
不会就选b1 小时前
算法日常・每日刷题--<BFS拓扑排序>4
算法
不正经学生1 小时前
C语言动态内存管理(上):堆上的自由与责任
c语言·开发语言·c++·算法·面试