C++ day2 练习

思维导图

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

定义公有成员函数:

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

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

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

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

复制代码
#include <iostream>

using namespace std;

class Rect {
private:
    int wide;
    int hight;
    int area;

public:
    void init(int w, int h){    //数据初始化函数

        wide = w;
        hight = h;
        area = wide * hight;
    }

    void set_w(int w){    //更改宽度的函数

        wide = w;
    }

    void set_h(int h){

        hight = h;
    }

    void show_area(){

        cout << "area is :" << wide * hight << endl;
    }

};

int main()
{
    int w,h;
    Rect r1;
    cout << "请依次输入长和宽 " << endl;
    cin >> w;
    cin >> h;
    r1.init(w,h);
    cout << "长方形的面积为:" << endl;
    r1.show_area();

    return 0;
}
相关推荐
8Qi81 天前
回文子串(Palindromic Substrings)—— 题解
算法·leetcode·职场和发展·动态规划
想吃火锅10051 天前
【leetcode】405.数字转换为十六进制数js
开发语言·javascript·ecmascript
专注VB编程开发20年1 天前
AI 生成C# WinForm 窗体 = 目前就是垃圾
开发语言·人工智能·c#
cfm_29141 天前
JVM GC垃圾回收初步了解
java·开发语言·jvm
~小先生~1 天前
Python从入门到放弃(一)
开发语言·python
许彰午1 天前
17_synchronized关键字深度解析
java·开发语言
z落落1 天前
C# 泛型接口和泛型类+泛型约束
开发语言·c#
阿正的梦工坊1 天前
【Rust】02-变量、不可变性与基础类型
开发语言·后端·rust
阿正的梦工坊1 天前
【Rust】08-集合类型、字符串与迭代器入门
开发语言·rust·c#