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;
}
相关推荐
想做小南娘,发现自己是女生喵6 分钟前
QT项目实战第一周(嵌入式)
开发语言·qt
学逆向的13 分钟前
C 可变量参数
c语言·开发语言·网络安全
andyweike14 分钟前
kotlin dsl library工程怎么配置
android·开发语言·kotlin
huaweichenai20 分钟前
php搭建mcp服务
开发语言·php·ai编程
张小姐的猫31 分钟前
【MySQL】数据类型
android·linux·数据库·c++·mysql·adb·php
估值探索者33 分钟前
【Python量化技术指标实战 02】MACD金叉也分强弱?手工算一次 DIF/DEA 就懂了
java·开发语言·python·股票数据·股票数据api接口
讳疾忌医丶34 分钟前
编译器眼中的 C++ 循环:从指针别名、IEEE 754 结合律到 SIMD 语义证明
开发语言·c++
重生六道44 分钟前
B站视频时长统计:一段JS脚本搞定分P/合集总时长(附倍速计算)
开发语言·javascript·音视频
XiYang-DING1 小时前
P5528.带过期时间的 LRU 缓存
java·开发语言·缓存
小徐不徐说1 小时前
C++ 机器视觉入门:从零搭建 AOI 开发环境(OpenCV + CMake)
开发语言·c++·opencv