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

实现效果

相关推荐
空堂与归几秒前
AI 与 SI(超级智能):先分清窄智能与超级智能的边界
开发语言·人工智能·gpt
熊猫钓鱼>_>1 分钟前
Kotlin Multiplatform for OpenHarmony 实战:为 kotlin-inject 实现依赖注入适配
开发语言·华为·kotlin·ai编程·inject·鸿蒙·openharmony
Wang's Blog21 分钟前
Java框架 SpringCloud 快速入门: 网关的 CORS 跨域配置
java·开发语言·spring cloud
驭渊的小故事25 分钟前
NC229023 题解:二分答案求解“最大化最小值“问题
java·算法
code2cat29 分钟前
【随笔】MCP工具错误怎样分层:先读反馈,再决定下一步
开发语言·后端·ai agent·mcp
朝朝辞暮i38 分钟前
C++ 第 36 课:Action——机械臂/VLA 非常重要
开发语言·c++·算法·ros2
无名猿38 分钟前
锁进阶:atomic_flag 自旋锁、shared_mutex 读写锁与无锁的代价
c++·性能优化·并发编程·现代c++
谢亮_vipxieliang41 分钟前
Go 接口设计原则核心知识点
开发语言·ios·golang
Wang's Blog42 分钟前
Java框架 SpringCloud 快速入门: Feign 替代 RestTemplate 实现声明式远程调用
java·开发语言·spring cloud
谢亮_vipxieliang42 分钟前
Go 结构体与方法集核心知识点
java·开发语言·golang