注册 登录
编程论坛 C++教室

一个c++程序

QQ_ABCD 发布于 2010-10-20 15:18, 525 次点击
大家帮忙看一下我的这个程序有什么错误,运行结果怎么为1,1,1,啊???
#include <iostream>
using namespace std;
class Box
{
public:
    Box(int h=10,int w=12,int len=15):height(h),width(w),length(len){}
    int volume();
private:
    int height;
    int width;
    int length;
};
int Box::volume()
{
    return (height*width*length);
}
int main()
{
    Box a[3]={
        Box(1,2,3),
        Box(4,5,6),
        Box(7,8,9)};
        cout<<a[0].volume<<endl;
        cout<<a[1].volume<<endl;
        cout<<a[2].volume<<endl;
        return 0;
}
4 回复
#2
sock2582010-10-20 15:27
cout<<a[0].volume()<<endl;
cout<<a[1].volume()<<endl;
cout<<a[2].volume()<<endl;
#3
lyj2010lyj2010-10-20 19:19
  int height;
    int width;
    int length;
#4
m21wo2010-10-20 19:58
volume()是类的成员函数一定要这么访问!a[0].volume(),否则按你所写是访问类的数据成员!
#5
QQ_ABCD2010-10-21 01:13
多谢各位
1