c++:编程类与对象问题

定义一个Box(盒子)类,在该类定义中包括数据成员:length(长),width(宽),和height(高)。成员函数:构造函数Box设置盒子长,宽,高三个数据;函数volume计算并输出盒子的体积。从键盘输出长,宽,高,并求盒子的体积

#include <stdio.h>

class Box
{
int length;
int width;
int height;
public:
Box(int a,int b,int c)
{
length=a;
width=b;
height=c;
}
void volume()
{
printf("The volume is:%d\n",length*width*height);
}
};

int main()
{
Box box(1,2,3);
box.volume();

return 0;
}
温馨提示:内容为网友见解,仅供参考
第1个回答  2009-05-18
class Box
{ private:
float length,width,height;
public:
Box(float l,float w,float h){ length=l;width=w;height=h;}
float volume(){ return (length*width*height); }
}
相似回答