2024/3/26 C++作业

定义一个矩形类(Rectangle),包含私有成员:长(length)、宽(width),

定义成员函数:

设置长度:void set_l(int l)

设置宽度:void set_w(int w)

获取长度:int get_l();

获取宽度:int get_w();

展示函数输出该矩形的周长和面积:void show()

cpp 复制代码
#include <iostream>
#include <iomanip>
#include <cstdlib>
using namespace std;
class Stu
{
private:

    int length;
    int width;
public:
    void set_l(int l);
    void set_w(int w);
    int get_l();
    int get_w();
    void show();
};

void Stu::set_l(int l)
{
    length = l;
}

void Stu::set_w(int w)
{
    width = w;
}
int Stu::get_l()
{
    return length;
}
int Stu::get_w()
{
    return width;
}
void Stu::show()
{
    int peremeter = (length+width)*2;
    int space = length*width;
    cout<<"该矩形的周长为:"<<peremeter<<" 该矩形的面积为:"<<space<<endl;
}

int main()
{
    Stu s1;
    s1.set_l(6);
    s1.set_w(8);
    s1.show();

    return 0;
}
相关推荐
xlq223224 小时前
22.多态(上)
开发语言·c++·算法
666HZ6664 小时前
C语言——高精度加法
c语言·开发语言·算法
sweet丶4 小时前
iOS MMKV原理整理总结:比UserDefaults快100倍的存储方案是如何炼成的?
算法·架构
星释4 小时前
Rust 练习册 100:音乐音阶生成器
开发语言·后端·rust
D_evil__4 小时前
[C++高频精进] 并发编程:线程基础
c++
风生u4 小时前
go进阶语法
开发语言·后端·golang
666HZ6664 小时前
C语言——黑店
c语言·开发语言
Gomiko5 小时前
JavaScript基础(八):函数
开发语言·javascript·ecmascript
云里雾里!5 小时前
力扣 209. 长度最小的子数组:滑动窗口解法完整解析
数据结构·算法·leetcode
〝七夜5695 小时前
JVM内存结构
java·开发语言·jvm