3-4 计算长方形的周长和面积 sdut-oj

来源:互联网 发布:linux查看文件系统类型 编辑:程序博客网 时间:2024/06/05 09:28

#include <iostream>

using namespace std;

class Rect
{
 private:
  double len,width;
 public:
  Rect(double a=0,double b=0);
  Rect(const Rect &r);
  void show(const char *name);
};

Rect::Rect(double a,double b)
{
 len =a;
 width=b;
}

Rect::Rect(const Rect &r)
{
 len=r.len;
 width=r.width;
}

void Rect::show(const char *name)
{
 cout<<"the length and width of "<<name<<" is:"<<len<<","<<width<<endl;
 cout<<"the perimeter of "<<name<<" is:"<<(len+width)*2.0<<endl;
 cout<<"the area of "<<name<<" is:"<<len*width<<endl;
}

int main()
{
 double x,y;
 cin>>x>>y;
 if(x<0||y<0)
 {
  x=0;
  y=0;
 }
 Rect r1(x,y);
 r1.show("r1");
 Rect r2=r1;
 r2.show("r2");
}