C++ Qt day2

自己封装一个矩形类(Rect),拥有私有属性:宽度(width)、高度(height),

定义公有成员函数:

初始化函数:void init(int w, int h)

更改宽度的函数:set_w(int w)

更改高度的函数:set_h(int h)

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

cpp 复制代码
#include <iostream>

using namespace std;

class Rect{
private:int width;

private:int height;

public:void init(int w,int h)
    {
        width=w;
        height=h;
        show();
    }

public:void set_w(int w)
    {
        width=w;
    }

public:void set_h(int h)
    {
        height=h;
    }

public:void show()
    {
        cout << "周长:" << 2*(height+width) << endl;
        cout << "面积:" << height*width << endl;
    }
};

int main()
{
    Rect rect;
    int width,height;
    cout << "请输入宽:";
    cin>>width;

    cout << "请输入高:";
    cin>>height;

    rect.init(width,height);
    cout << "更改宽:";
    cin>>width;

    rect.set_w(width);
    cout << "更改高:";
    cin>>height;

    rect.set_h(height);
    rect.init(width,height);

    return 0;
}
相关推荐
小屁猪qAq8 分钟前
设计模式总纲
开发语言·c++·设计模式
不绝19114 分钟前
C#核心:多态
开发语言·c#
Howrun77714 分钟前
C++标准线程库-全面讲解
开发语言·c++
浪扼飞舟16 分钟前
C#(多线程和同步异步)
java·开发语言
万行16 分钟前
机器人系统SLAM讲解
开发语言·python·决策树·机器学习·机器人
抬头望远方17 分钟前
【无人机】无人机群在三维环境中的碰撞和静态避障仿真(Matlab代码实现)
开发语言·支持向量机·matlab·无人机
matlab科研助手19 分钟前
【路径规划】基于遗传算法的农药无人机在多边形区域的路径规划研究附Matlab代码
开发语言·matlab·无人机
2301_7806698622 分钟前
字符集及其编码、解码操作、IO流分类
java·开发语言
无名的小三轮30 分钟前
第三章 防火墙概述
开发语言·php
有梦想的攻城狮38 分钟前
Java中的Double类型的存在精度丢失详解
java·开发语言·bigdecimal·double