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

实现效果

相关推荐
吃好睡好便好7 小时前
用while循环语句求和
开发语言·学习·算法·matlab·信息可视化
TechWayfarer7 小时前
查询IP所在地的3种方案:从API到离线库,风控场景怎么选?
开发语言·网络·python·网络协议·tcp/ip
摇滚侠7 小时前
Java 零基础全套教程,集合框架,笔记 153-163
java·开发语言·笔记
王璐WL7 小时前
【C语言入门级教学】函数的概念2
c语言·数据结构·算法
程序员榴莲8 小时前
Python 单例模式
开发语言·python·单例模式
不知名的忻8 小时前
B 树与 B+ 树:面试完全指南
b树·算法·面试·b+树
L、2188 小时前
CANN算子开发调试实战:从“Segmentation Fault“到定位根因的完整流程
java·开发语言
狗凯之家源码网8 小时前
基于PHP的多语言跨境电商B2B2C商城系统技术解析
开发语言·php
运筹vivo@9 小时前
2657. 找到两个数组的前缀公共数组 | 难度:中等
算法·leetcode·职场和发展·哈希表
索木木9 小时前
NCCL SHARP 和 TREE算法
java·服务器·算法