类与对象实验报告
实验内容
- 定义矩形类Rectangle,该类有3个私有数据成员double length(长)、width(宽)、area(面积)。定义如下公有成员函数:
构造函数,完成对象初始化;复制构造函数,完成对象复制;析构函数,显示调用了析构函数。
void change(double,double) 函数,改变矩形的长、宽,并计算面积。
void showpara()函数, 显示Rectangle对象数据。
在主函数中建立Rectangle对象,调用各函数。
源程序:
#include
using namespace std;
class
Rectangle{
private:
double length,width,area;
public:
Rectangle(double, double);
Rectangle(Rectangle& f);
~Rectangle();
void change(double,double);
showpara()
{
cout<<“length:”<<length<<endl;
cout<<“width:”<<width<<endl;
cout<<“area:”<<area<<endl;
}
};
Rectangle::Rectangle(double
len,double wid)
{
length=len;
width=wid;
area=length*width;
}
Rectangle::Rectangle(Rectangle& f)
{
this->length=f.length;
this->width=f.width