试编写一个基于对象的程序,求3个长方体的体积。
试编写一个基于对象的程序,求3个长方体的体积。长方体对象名为rectangle,数据成员包括:length,width,height。要求用成员函数实现以下功能:①有键盘分别输入3个长方体的长、宽、高;
②计算其长方体之体积;
③输出3个长方体的体积。
程序代码:class rectangle
{
private:
int length,width,height;
public:
void input();
long volume();
void print();
}
void rectangle::input()
{
cout <<"input length,width,height:";
cin >> length >> width >> height;
}
long rectangle::volume()
{
return length*width*height;
}
void rectangle::print()
{
cout << "V=" << rectangle()<< endl;
}
int main()
{
rectangle rect;
rect.input();
rect.print();
return 0;
}
