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;
}
相关推荐
破茧蝴蝶7 小时前
Python分布式爬虫完整教程
开发语言·python
狗凯之家源码网8 小时前
全新 PHP 轻量客服聊天室源码深度评测
开发语言·php·聊天室系统
远离UE48 小时前
UE5 Substrate Thin Film 源码拆析
开发语言·javascript·ue5
keyipatience8 小时前
JavaScript学习(1)书写位置,变量、数据类型、运算符
开发语言·前端·javascript·css·学习
一拳一个呆瓜8 小时前
【STL】文件系统导航
c++·stl
All for pursuit.9 小时前
【动态规划-21】312.戳气球
数据结构·c++·算法·leetcode·动态规划
栈帧之外9 小时前
【C++】STL之set
开发语言·c++
catchadmin10 小时前
免费可商用 PHP 管理后台 CatchAdmin V5.4.1 发布,新增 MCP 支持
开发语言·php
zhanghaha131410 小时前
C++构造函数的一些其他应用(逐词逐句保姆讲解)
c++
jimy111 小时前
c++的 Type Traits (二、更详细版本)
开发语言·c++