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;
}
相关推荐
hehelm1 分钟前
仿muduo库实现高并发服务器—EventLoop
linux·服务器·网络·c++
_Narcissus_1 分钟前
常见数论算法笔记
数据结构·c++·算法·高精度·数论·快速幂·质数筛
余额瞒着我当琳3 分钟前
C++STL--list底层实现,迭代器分类,模拟list的迭代器封装、实现
java·开发语言·c++
小陈的进阶之路5 分钟前
Claude Code辅助测试:API测试与pytest自动化
android·开发语言·kotlin
denggun123457 分钟前
Python两套原生信号量与swift对比
开发语言·python·swift
kyle~12 分钟前
计算机系统 --- 缓存一致性
开发语言·c++·缓存·计算机系统
李妍.8 小时前
02Numpy基础(上)
开发语言·python
TheBestRucy9 小时前
Python 九阳神功之贰:面向对象(下)
开发语言·python
吃着火锅x唱着歌9 小时前
Effective C++ 学习笔记 条款43 学习处理模板化基类内的名称
c++·笔记·学习
AI_小站9 小时前
刚面完百度的 Agent 开发岗,我才发现:世界就是个巨大的草台班子
java·开发语言·人工智能·spring·百度·langchain