C++ day2

1.xmind
2.

cpp 复制代码
#include <iostream>

using namespace std;

class Rect
{
private:
    int  width;
    int height;
public:
    void init(int width,int height );
    void set_width(int width);
    void set_height(int height);
    void show();
};
void Rect :: init (int width,int height)
{
    this->width=width;
    this->height=height;
}
void Rect :: set_width(int width)
{
    this->width=width;
}
void Rect :: set_height(int height)
{
    this->height=height;
}
void Rect :: show()
{
    cout << "周长: " << 2*height+2*width << "  面积: " << height*width << endl ;
}

int main()
{
    int width = 10;
    int height =10 ;
    Rect r1;
    r1.init(width,height);
    r1.show();

    cout << "please enter width : " ;
    cin >> width ;
    r1.set_width(width);
    cout << "please enter height : ";
    cin >> height;
    r1.set_height(height);
    r1.show();

    return 0;
}
相关推荐
白杨尚青2 小时前
C++入门篇(十一):string(中)——容量与增删改查:一篇吃透所有常用接口(万字详解)
java·开发语言·c++
朔北之忘 Clancy2 小时前
2024 CSP-J 第二轮真题解析
c++·算法·青少年编程·noip·csp·信奥赛·noi
Lazionr2 小时前
set和map的使用——从基础接口到实际应用
c++
重生之小比特2 小时前
【C++进阶】哈希表实现
开发语言·c++·哈希
m0_380743872 小时前
VisualStudio中OpenCV的创建与配置使用小结
开发语言·c++
凉茶钱2 小时前
【C++】动态内存管理完整解析:从内存划分到 new delete 底层原理
c语言·开发语言·c++·内存管理·动态内存
cu1433 小时前
细谈GM8775C的具体功能和应用
c语言·c++·人工智能·嵌入式硬件
无名猿3 小时前
std::optional 完全指南:别再用 -1 和 nullptr 表达「没有值」
c++·标准库·现代c++·语法基础
无名猿3 小时前
nullptr 取代 NULL 和 0:空指针的三种写法和唯一正确解
c++·现代c++·语法基础·踩坑记录
大侠归来3 小时前
C++ 与 Lua 的协程交互:从原理到实战
c++·lua·交互