C++Day2

cpp 复制代码
#include <iostream>

using namespace std;


class Rect
{
private:
    int width;
    int height;
public:
    void init(int w,int h)
    {
        width = w;
        height = h;
    }
    void set_w(int w)
    {
        width = w;
    }
    void set_h(int h)
    {
        height = h;
    }
    void show()
    {
        cout << "矩形的周长为:" << (width + height) * 2 << endl;
        cout << "矩形的面积为:" << width * height << endl;
    }
};
int main()
{
   Rect r1;
   r1.init(15,20);
   r1.show();
   r1.set_h(5);
   r1.set_w(8);
   r1.show();
    return 0;
}
相关推荐
I_belong_to_jesus1 小时前
std::unique_ptr成员函数用法
c++
Tairitsu_H2 小时前
[C++] 拷贝构造还是赋值重载?类默认成员函数细节详解
开发语言·c++·类和对象
goodlook01232 小时前
opencode调用本地模型一(1.18.30版本安装)
开发语言
菜鸟~noob2333 小时前
【电子战】第14篇:TOA 定位——圆交汇与到达时间【含matlab代码】
开发语言·matlab
汉克老师3 小时前
GESP2026年9月认证C++四级( 第一部分选择题(1~7题)精讲
c++·gesp·小学生·学c++编程
itmigrate3 小时前
Redis 开发高频隐形踩坑记录
开发语言·java-ee
geats人山人海3 小时前
现代c++第二章 字符串
c++
白远山3 小时前
本地游戏代练源码开发实战:架构设计与核心功能实现指南
java·开发语言·架构·需求分析
idealzouhu3 小时前
现代 CMake 依赖管理详解:从构建树、Target 封装到 find_package 工作原理
c++·cmake
aramae4 小时前
Python 使用库:标准库与第三方库实战
服务器·开发语言·windows·python