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;
}
相关推荐
我也要当昏君1 天前
5.3 【2012统考真题】
开发语言·智能路由器·php
木木木丫1 天前
嵌入式项目:韦东山驱动开发第六篇 项目总结——显示系统(framebuffer编程)
c语言·c++·驱动开发·dsp开发
柳鲲鹏1 天前
RGB转换为NV12,查表式算法
linux·c语言·算法
橘颂TA1 天前
【剑斩OFFER】算法的暴力美学——串联所有单词的字串
数据结构·算法·c/c++
Kuo-Teng1 天前
LeetCode 73: Set Matrix Zeroes
java·算法·leetcode·职场和发展
初见无风1 天前
3.4 Boost库intrusive_ptr智能指针的使用
开发语言·boost
mit6.8241 天前
[HDiffPatch] 补丁算法 | `patch_decompress_with_cache` | `getStreamClip` | RLE游程编码
c++·算法
程序猿20231 天前
Python每日一练---第六天:罗马数字转整数
开发语言·python·算法
葵续浅笑1 天前
LeetCode - 杨辉三角 / 二叉树的最大深度
java·数据结构·算法·leetcode