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;
}
相关推荐
残影飞雪5 分钟前
Jetson版本下Pytorch和torchvision
c++
松涛和鸣1 小时前
14、C 语言进阶:函数指针、typedef、二级指针、const 指针
c语言·开发语言·算法·排序算法·学习方法
yagamiraito_3 小时前
757. 设置交集大小至少为2 (leetcode每日一题)
算法·leetcode·go
星释3 小时前
Rust 练习册 57:阿特巴什密码与字符映射技术
服务器·算法·rust
无敌最俊朗@3 小时前
力扣hot100-141.环形链表
算法·leetcode·链表
智商低情商凑5 小时前
Go学习之 - Goroutines和channels
开发语言·学习·golang
半桶水专家5 小时前
Go 语言时间处理(time 包)详解
开发语言·后端·golang
编程点滴5 小时前
Go 重试机制终极指南:基于 go-retry 打造可靠容错系统
开发语言·后端·golang
实心儿儿5 小时前
C++ —— 模板进阶
开发语言·c++
WWZZ20256 小时前
快速上手大模型:深度学习10(卷积神经网络2、模型训练实践、批量归一化)
人工智能·深度学习·神经网络·算法·机器人·大模型·具身智能