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;
}
相关推荐
愚润求学12 分钟前
【Linux】进程间通信(一):认识管道
linux·运维·服务器·开发语言·c++·笔记
珊瑚里的鱼27 分钟前
【滑动窗口】LeetCode 1658题解 | 将 x 减到 0 的最小操作数
开发语言·c++·笔记·算法·leetcode·stl
晚秋大魔王40 分钟前
OpenHarmony 开源鸿蒙南向开发——linux下使用make交叉编译第三方库——wget
java·linux·运维·开发语言·华为·harmonyos
heath ceTide43 分钟前
轻量、优雅、高扩展的事件驱动框架——Hibiscus-Signal
java·开发语言
_extraordinary_44 分钟前
Java 常用的Arrays函数
java·开发语言
_extraordinary_1 小时前
Java 类和对象
java·开发语言
Aliano2171 小时前
TestNGException ClassCastException SAXParserFactoryImpl是Java自带的Xerces解析器——解决办法
java·开发语言·python
共享家95271 小时前
哈希的原理、实现
c++·算法
漫谈网络1 小时前
回调函数应用示例
开发语言·python·回调函数
亚林瓜子1 小时前
pyenv简单的Python版本管理器(macOS版)
开发语言·python·macos·pyenv