c++关于类数组赋值小问题,看看这程序哪里错了

来源:百度知道 编辑:UC知道 时间:2024/07/02 22:57:36
#include <iostream>
using namespace std;
class Box
{public:
Box(int h=10,int w=10,int len=10):height(h),width(w),length(len){}
int get_height()
{
return height;
}
int get_width()
{
return width;
}
int get_length()
{
return length;
}
private:
int height;
int width;
int length;
};
int main()
{Box b[3];
int nh,nw,nlen,i;
for(i=0;i<3;i++)
{cout<<"请输入年月日";
cin>>nh>>nw>>nlen;
b[i]=Box(nh,nw,nlen);
}
for(i=0;i<3;i++)
cout<<"b["<<i<<"]="<<b[i].get_height()<<" "<<b[i].get_width()<<" "<<b[i].get_length<<endl;
getchar();
getchar();
return 0;
}

cout<<"b["<<i<<"]="<<b[i].get_height()<<" "<<b[i].get_width()<<" "<<b[i].get_length<<endl;
b[i].get_length少了对括号,其他地方好像没发现有问题,提问的时候顺便把你遇到的问题一起说一下。

b[i].get_length少了对括号
最主要的问题是你for应该是不会出现你想要的结果的,因为Box b[3]只会调用默认构造函数也就是Box();你想要让
int height;
int width;
int length;
都初始化为10的话,必须这样写Box():height(10),width(10),length(10){}