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

实现效果

相关推荐
Dlrb12112 小时前
C语言-指针三
c语言·算法·指针·const·命令行参数
kkeeper~2 小时前
0基础C语言积跬步之深入理解指针(5下)
c语言·开发语言
Tisfy2 小时前
LeetCode 2540.最小公共值:双指针(O(m+n))
算法·leetcode·题解·双指针
一直不明飞行2 小时前
Java的equals(),hashCode()应该在什么时候重写
java·开发语言·jvm
IronMurphy3 小时前
【算法四十七】152. 乘积最大子数组
算法
REDcker3 小时前
有限状态机与状态模式详解 FSM建模Java状态模式与C++表驱动模板实践
java·c++·状态模式
盲敲代码的阿豪3 小时前
Python 入门基础教程(爬虫前置版)
开发语言·爬虫·python
basketball6163 小时前
C++ 构造函数完全指南:从入门到进阶
java·开发语言·c++
互联科技报3 小时前
2026超融合选型:Top5品牌与市场格局解读
开发语言·perl
weixin199701080164 小时前
[特殊字符] 智能数据采集:数字化转型的“数据石油勘探队”(附Python实战源码)
开发语言·python