2 回答
TA贡献1780条经验 获得超1个赞
正确的程序为:
#include<iostream.h>
class rectangle
{
protected:
double length;
double width;
public:
rectangle::assign(double len,double wide);
rectangle(){assign(0,0);}
rectangle(double len,double wide){assign(len,wide);}
double getLength(){return length;}
double getWidth(){return width;}
double getArea(){return length*width;}
};
rectangle::assign(double len,double wide)
{
length=len;width=wide;
}
int main()
{
rectangle rect;
double len,wide;
cout<<"enter length of rectangle:";
cin>>len;
cout<<"enter width of rectangle:";
cin>>wide;
rect.assign(len,wide);
cout<<"rectangle length="<<rect.getLength()<<"\n"
<<" width ="<<rect.getWidth()<<"\n"
<<" area ="<<rect.getArea()<<"\n";
return 0;
}
在类定义外定义函数时需在类内做函数声明。
另void rectangle::assign(double len,double wide) 类声明重复,这里rectangle是定义的类,是你需要的,所以void不要。还有main( )前必须有类声明,这里为int。
- 2 回答
- 0 关注
- 929 浏览
添加回答
举报