定义一个矩形类(Rectangle),包含私有成员:长(length)、宽(width),
定义成员函数:
设置长度:void set_l(int l)
设置宽度:void set_w(int w)
获取长度:int get_l();
获取宽度:int get_w();
展示函数输出该矩形的周长和面积:void show()
#include <iostream>
using namespace std;
class Rectangle {
private:int length;int width;
public:void set_l(int l) {length = l;}void set_w(int w) {width = w;}int get_l(){return length;}int get_w(){return width;}void show(){int c = 2 * (length + width);int area = length * width;cout<<"Rectangle 周长 = "<<c<<endl;cout<<"Rectangle 面积 = "<<area<<endl;}
};
int main()
{Rectangle rect;rect.set_l(10);rect.set_w(4);cout<<"长 = "<<rect.get_l()<<endl;cout<<"宽 = "<<rect.get_w()<<endl;rect.show();return 0;
}