运用面向对象程序设计方法实现一个长方形类Rect,要求能设定长方形的长和宽,能计算长方形的面积和周长,在main中创建一个长方形。
#include"iostream"
using namespace std;
class Rect
{
private:
double length;
double width;
public:
void side(double l,double w);
void Area();
void Peri();
};
void Rect::side(double l,double w)
{
length=l;
width=w;
}
void Rect::Area()
{
cout<<"Area="<<length*width<<endl;
}
void Rect::Peri()
{
cout<<"Peri="<< 2*(length+width)<<endl;
}
int main()
{
Rect r;
r.side(4,7);
r.Area();
r.Peri();
return 0;
}