c++day2

1.XMIND

自己封装一个矩形类(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 set_w(int w);//设置宽度
    void set_h(int h);//设置高度
    int get_w();//获取宽度
    int get_h();//获取高度
    void show();//显示周长和面积
};

int main()
{
    Rect r;//实例化了一个Rect类的类对象r
    r.set_w(10);//设置宽度
    r.set_h(3);//设置高度
    cout << "宽度:" << r.get_w() << endl;
    cout << "高度:" << r.get_h() << endl;
    r.show();
    return 0;
}

void Rect::set_w(int w)
{
    width = w;
}
void Rect::set_h(int h)
{
    height = h;
}
int Rect::get_w()
{
    return width;
}
int Rect::get_h()
{
    return height;
}

void Rect::show()
{
    cout << "周长 = " << (width+height)*2 << endl;
    cout << "面积 = " << width*height << endl;
}
相关推荐
bkspiderx11 分钟前
Qt 插件机制:动态扩展应用功能的核心框架
开发语言·qt·元数据·qt 插件·qpluginloader
程序员老陆22 分钟前
深入理解 C++ thread_local:线程私有存储的正确打开方式
开发语言·c++·程序设计
沐晓时光28 分钟前
C语言入门,深入理解指针(3)
c语言·开发语言
码云数智-园园1 小时前
unique_ptr 还是 shared_ptr?C++ 智能指针选型与内存泄漏实战分析
java·开发语言
禾小西2 小时前
07丨Redis 哨兵机制:主库故障后,如何恢复服务?
java·开发语言·redis
我不是阵雨2 小时前
JDK 21虚拟线程Pinning陷阱:一文拔钉解困
java·开发语言
徐小黑ACG2 小时前
Golang 基础05 结构体struct
开发语言·算法·golang
优橙教育3 小时前
零基础学AI应用开发要多久?3个月能到什么水平
服务器·开发语言·网络·php
SEO_juper3 小时前
用 Python 写一个 GEO 可见性检查脚本:你的网站现在能被 AI 引用吗
开发语言·人工智能·爬虫·python·seo·外贸独立站
西柚小萌新3 小时前
【LLM&&AI应用开发 八股文】--4.3.Agent智能体(下)
java·开发语言·数据库