定义一个矩形类,有长、宽两个属性,有成员函数计算矩形的面积
#include <iostream>
using namespace std;
class Rectangle
{
public:
Rectangle(float len,float width)
{
Length=len;
Width=width;
}
~Rectangle(){};
float GetArea(){return Length *Width;}
float GetLength(){return Length;}
float GetWidth(){return Width;}
private:
float Length;
float Width;
};
void main()
{
float length,width;
cout<<"请输入矩形的长度:";
cin>>length;
cout<<"请输入矩形的宽度:";
cin>>width;
Rectangle r(length,width);
cout<<"长为"<< length<<"宽为"<< width<<"的矩形的面积为:"<<r.GetArea()<<endl;
}
![在这里插入图片描述](https://i-blog.csdnimg.cn/blog_migrate/fee4b6b2a16af1f5a19e4fd138267f5e.png)