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

实现效果

相关推荐
夏天的清晨3 分钟前
C++入门学习
开发语言·c++·学习
新鲜势力呀6 分钟前
PHP 文件上传系统实战:从上传卡顿到分片上传 + 秒传 + 云存储架构完整优化方案
开发语言·架构·php
汉克老师7 分钟前
GESP2026年9月认证C++七级( 第二部分判断题(1~10题)精讲
c++·gesp·小学生·学c++编程
Dream it possible!10 分钟前
蓝桥杯_翻转_C++
c++·算法·蓝桥杯
Ivanqhz10 分钟前
BURG(自底向上重写生成器)
服务器·数据库·人工智能·深度学习·算法
牵猫散步的鱼儿10 分钟前
C 和 C++ 区别
c++
言乐617 分钟前
逻辑回归与利弊
人工智能·算法·机器学习·数据挖掘·逻辑回归
程序员老陆22 分钟前
从语法糖到类型系统:深度拆解 C++ Lambda 与可调用对象的“血缘关系”
开发语言·c++·程序设计
(Charon)23 分钟前
【C++面试】设计模式:手写单例模式与工厂模式
开发语言·c++
估值探索者24 分钟前
【Python量化系统工程实战 #01】数据存储选型 CSVSQLiteMySQL 对比与 SQLite 实战建库
开发语言·jvm·python·sqlite·api接口·数据api接口·股票数据api接口