对象的应用

来源:百度知道 编辑:UC知道 时间:2024/05/03 06:16:52
实验要求:定义一个矩形类,数据成员包括:长和宽。设计两个成员函数,分别实现长、宽的输入和求矩形的面积,并自己拟定主函数进行验证,程序要求调试通过。

#include <iostream>
using std::cout;
using std::endl;

class Rectangle{
public:
void setDimension(double l,double w){length=l;width=w;}
double getArea(){return length*width;}
private:
double length;
double width;
};

int main(){
Rectangle a;
a.setDimension(15,10);
cout<<"The area of the rectangle is "<<a.getArea()<<endl;
return 0;
}