(1) 该类中的私有成员变量存放CRectangle的长和宽,并且设置它们的默认值为1.
(2) 通过成员函数设置其长和宽,并确保长和宽都在(0,50)范围之内。
(3) 求周长Perimeter
输入格式:输入在一行中给出2个绝对值不超过50的浮点数A和B。
输出格式:在一行中输出周长的值
#include<iostream>
#include<iomanip>
using namespace std;
class CRectangle
{
public:
void setlw();
float getper();
private:
float l, w;
};
void CRectangle::setlw()
{
cin >> l;
if (l > 50)l = 1;
cin >> w;
if (w > 50)w = 1;
}
float CRectangle::getper()
{
return 2*(l+w);
}
int main()
{
CRectangle a;
a.setlw();
cout << a.getper();
}