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

实现效果

相关推荐
Mininglamp_2718几秒前
Claude Code 封禁中国开发者之后:本地 AI 编程工具的替代方案实测
开发语言·人工智能·windows·开源软件·ai-native
思麟呀11 分钟前
C++17(三)if constexpr+折叠表达式
开发语言·c++
醉城夜风~12 分钟前
Java详解经典算法题:接雨水(三种实现方案+原理剖析)
java·开发语言·算法
ysa05103021 分钟前
【板子】ST表
c++·笔记·算法·板子
qydz1122 分钟前
杰理开发基础知识(3)
开发语言·嵌入式开发·杰理科技
贾斯汀frank22 分钟前
C# 15 类型系统改进:Union Types
开发语言·windows·c#
阿里嘎多学长34 分钟前
2026-07-10 GitHub 热点项目精选
开发语言·程序员·github·代码托管
CHHH_HHH1 小时前
【C++11】深入解析C++可变参数模板
开发语言·c++·算法·stl·c++11
hurrycry_小亦1 小时前
洛谷题目:P1215 [USACO1.4] 母亲的牛奶 Mother‘s Milk 题解(本题简)
算法
十五年专注C++开发1 小时前
fatal error C1189: #error: WinSock.h has already been included 的解决方案
c++·socket·boost