计算长方形的周长和面积

用到的知识点是
1、构造函数的初始化
2、拷贝构造函数,形参为对象的常引用

#include <iostream>
using namespace std;
class Rect
{
private:
    double len;
    double wid;
public:
    Rect(double x=0,double y=0);
    Rect(const Rect &b);              //拷贝构造函数,形参为对象的常引用
    const void display()
    {
        cout<<"the length and width of r1 is:"<<len<<","<<wid<<endl;
        cout<<"the perimeter of r1 is:"<<(len+wid)*2<<endl;
        cout<<"the area of r1 is:"<<len*wid<<endl;

    }
    const void display1()
    {
        cout << "the length and width of r2 is:" << len << ',' << wid<< endl;
        cout << "the perimeter of r2 is:" << (len + wid) * 2 << endl;
        cout << "the area of r2 is:" << len * wid << endl;
    }
};
Rect::Rect(double x,double y)
{
    len=x;
    wid=y;
}
Rect::Rect(const Rect &b)
{
    len=b.len;
    wid=b.wid;
}
int main()
{
    double x,y;
    cin>>x>>y;
    if(x<0||y<0)
    {
        x=0;
        y=0;
    }
    Rect rect(x,y);
    Rect rect1=rect;
    rect.display();
    rect1.display1();
    return 0;
}
Fork me on GitHub