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

实现效果

相关推荐
denlaku2 分钟前
JDK 13 新特性详解
java·开发语言
念越14 分钟前
题库、练习与错题本代码阅读
开发语言·数据结构
l1t16 分钟前
几种数据库软件count(distinct)和count()from group by子查询性能测试
开发语言·数据库
zhenaibo52120 分钟前
如何与导师有效沟通选题?
算法
熊猫钓鱼>_>25 分钟前
大学图论第 6 讲:网络流与二分图匹配——对偶性让瓶颈自己现形
开发语言·算法·面试·职场和发展·图论·校招·面经
方便面不加香菜26 分钟前
C++ 关于多态的知识点讲解
开发语言·c++
没有钱的钱仔31 分钟前
最大似然估计(MLE)通俗理解
算法·机器学习·概率论
yi01138 分钟前
LeetCode 424. 替换后的最长重复字符|不定长滑动窗口:如何判断窗口是否合法?
笔记·python·算法·leetcode·双指针·滑动窗口
jimy11 小时前
C++ 类成员函数的内联定义(inline)详解
开发语言·c++
Nebula_g1 小时前
JavaSE高级:动态代理
java·开发语言·算法·javase