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

实现效果

相关推荐
Hello--_--World1 分钟前
JS:this指向、bind、call、apply、知识点与相关面试题
开发语言·javascript·ecmascript
沐知全栈开发24 分钟前
CSS Text(文本)
开发语言
会编程的土豆27 分钟前
【日常做题】 代码随想录(岛屿最大面积+寻宝)
数据结构·算法·图论
前进吧-程序员29 分钟前
现代 C++ 异步编程:从零实现一个高性能 ThreadPool (C++20 深度实践)
开发语言·c++·c++20
阿洛学长33 分钟前
汉洛塔结构思维
算法
木子n140 分钟前
第2篇:坐标变换与数学基础:FOC算法的核心数学工具
算法·电机控制·foc
Rsun0455142 分钟前
10、Java 桥接模式从入门到实战
java·开发语言·桥接模式
jieyucx1 小时前
Golang 完整安装与 VSCode 开发环境搭建教程
开发语言·vscode·golang
阿Y加油吧1 小时前
两道经典 DP 题:零钱兑换 & 单词拆分(完全背包 + 字符串 DP)
算法
pearlthriving1 小时前
c++当中的泛型思想以及c++11部分新特性
java·开发语言·c++