编写一个矩形类,数据成员为长和宽,函数成员包括计算周长函数、计算面积函数、构造函数、析构函数。在主函数中定义该类对象,调用相应的函数并输出。

编写一个矩形类,数据成员为长和宽,函数成员包括计算周长函数、计算面积函数、构造函数、析构函数。在主函数中定义该类对象,调用相应的函数并输出。


#include <iostream>
using namespace std;
class Rectangle
{

public:
    double width,length;
    double getZc(){return 2*(width+length);}
    double getMj(){return width*length;}
    Rectangle(){}
    Rectangle(int w,int l){width = w;length = l;}
    ~Rectangle(){}
};

int main()
{
    Rectangle rc(2,3);
    cout << "周长=" << rc.getZc()<<endl;
    cout <<"面积=" << rc.getMj()<<endl;
    return 0;
}