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;
}
相关推荐
暮雪倾风11 分钟前
【JS-Node】node.js环境安装及使用
开发语言·javascript·node.js
Dxy12393102167 小时前
Python 使用正则表达式将多个空格替换为一个空格
开发语言·python·正则表达式
故事和你919 小时前
洛谷-数据结构1-1-线性表1
开发语言·数据结构·c++·算法·leetcode·动态规划·图论
脱氧核糖核酸__9 小时前
LeetCode热题100——53.最大子数组和(题解+答案+要点)
数据结构·c++·算法·leetcode
脱氧核糖核酸__9 小时前
LeetCode 热题100——42.接雨水(题目+题解+答案)
数据结构·c++·算法·leetcode
techdashen10 小时前
Rust项目公开征测:Cargo 构建目录新布局方案
开发语言·后端·rust
星空椰10 小时前
JavaScript 进阶基础:函数、作用域与常用技巧总结
开发语言·前端·javascript
忒可君10 小时前
C# winform 自制分页功能
android·开发语言·c#
Rust研习社10 小时前
Rust 智能指针 Cell 与 RefCell 的内部可变性
开发语言·后端·rust
王老师青少年编程10 小时前
csp信奥赛C++高频考点专项训练之贪心算法 --【线性扫描贪心】:数列分段 Section I
c++·算法·编程·贪心·csp·信奥赛·线性扫描贪心