11.29 C++ 作业

自己封装一个矩形类(Rect),拥有私有属性:宽度(width)、高度(height),

定义公有成员函数:

初始化函数:void init(int w, int h)

更改宽度的函数:set_w(int w)

更改高度的函数:set_h(int h)

输出该矩形的周长和面积函数:void show()

cpp 复制代码
#include <iostream>

using namespace std;

class Rect
{
private:
    int width;
    int height;
public:
    void init(int w, int h);
    int set_w(int w);
    int set_h(int h);
    void show();
};

void Rect::init(int w, int h)
{
    this->width = w;
    this->height = h;
}

int Rect::set_w(int w)
{
    cout << "请输入修改的width:";
    cin >> w;
    return w;
}

int Rect::set_h(int h)
{
    cout << "请输入修改的height:";
    cin >> h;
    return h;
}

void Rect::show()
{
    cout << "C = " << (width+height)*2 << endl;
    cout << "S = " << width*height << endl;
}
int main()
{
    Rect r1;
    r1.init(0,0);
    cout << "初始为:" << endl;
    r1.show();
    int w = r1.set_w(w);
    int h = r1.set_h(h);
    r1.init(w,h);
    cout << "修改后为:" << endl;
    r1.show();
    return 0;
}
相关推荐
海奥华22 分钟前
go中的接口返回设计思想
开发语言·后端·golang
lifallen4 分钟前
深入浅出 Arrays.sort(DualPivotQuicksort):如何结合快排、归并、堆排序和插入排序
java·开发语言·数据结构·算法·排序算法
运维开发王义杰4 分钟前
Python: 告别 ModuleNotFoundError, 解决 pipx 环境下 sshuttle 缺少 pydivert 依赖的终极指南
开发语言·python
jingfeng5144 分钟前
数据结构排序
数据结构·算法·排序算法
k要开心5 分钟前
从C到C++语法过度1
开发语言·c++
小吕学编程8 分钟前
策略模式实战:Spring中动态选择商品处理策略的实现
java·开发语言·设计模式
whoarethenext15 分钟前
使用 C/C++的OpenCV 实时播放火柴人爱心舞蹈动画
c语言·c++·opencv
q5673152329 分钟前
IBM官网新闻爬虫代码示例
开发语言·分布式·爬虫
能工智人小辰30 分钟前
Codeforces Round 509 (Div. 2) C. Coffee Break
c语言·c++·算法
kingmax5421200831 分钟前
CCF GESP202503 Grade4-B4263 [GESP202503 四级] 荒地开垦
数据结构·算法