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

实现效果

相关推荐
Jun6263 分钟前
QT(14)-UBUNTU下QT使用串口
开发语言·qt·ubuntu
Jun6263 分钟前
QT(16)-云端版本管理
开发语言·qt
ggaofeng4 分钟前
试用zeroclaw
java·开发语言
插件开发10 分钟前
矢量路径运算如何选GPU技术?——适用算法对比及OpenGL/Direct3D/CUDA选型指南
算法·3d
~|Bernard|16 分钟前
关于go语言中二维切片的append操作陷阱
开发语言·后端·golang
c++之路19 分钟前
C/C++ 全链路编译工具汇总
c语言·开发语言·c++
c2385620 分钟前
C++的IO流深入理解(下)
开发语言·c++
8Qi820 分钟前
LeetCode 72:编辑距离(Edit Distance)—— 题解
算法·leetcode·职场和发展·动态规划
Cloud_Shy61821 分钟前
解读《Effective Python 3rd Edition》:从练气到老魔(第四章 Item 27 - 29)
开发语言·人工智能·经验分享·python·学习方法
简简单单lym24 分钟前
WebRTC进阶--red+ulpfec深度解析3-FEC--冗余控制机制深度解析
开发语言·webrtc