上海专业网站建设市场,惠州市+网站开发公司,容桂免费网站建设公司,一站式网站开发自己封装一个矩形类(Rect)#xff0c;拥有私有属性:宽度(width)、高度(height)#xff0c;
定义公有成员函数:
初始化函数:void init(int w, int h)
更改宽度的函数:set_w(int w)
更改高度的函数:set_h(int h)
输出该矩形的周长和面积函数:void show()
#ifndef RECT_H
…自己封装一个矩形类(Rect)拥有私有属性:宽度(width)、高度(height)
定义公有成员函数:
初始化函数:void init(int w, int h)
更改宽度的函数:set_w(int w)
更改高度的函数:set_h(int h)
输出该矩形的周长和面积函数:void show()
#ifndef RECT_H
#define RECT_H
#include iostream
using namespace std;
class Rect
{
private:int width;int height;public:void init(int w,int h);void set_w(int w);void set_h(int h);void show();
};#endif // RECT_H
#includerect.h
using namespace std;int main()
{Rect s1;int w5;int h5;s1.init(5,5);s1.set_w(w);s1.set_h(h);s1.show();return 0;
}
#include rect.hvoid Rect::init(int w,int h){Rect::widthw;Rect::heighth;}void Rect::set_w(int w){Rect::widthw;}void Rect::set_h(int h){Rect::heighth;}void Rect::show(){cout 矩形周长 this-widththis-widththis-heightthis-height endl;cout 面积 this-width*this-height endl;}