对象数组的使用

#include <iostream>
using namespace std;
class Box
{
    public:
    Box(int h = 10,int w = 12,int len = 15):height(h),width(w),length(len){};
//等于以下赋值情况
/** 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(2,3,4),Box(4,5,6)};
    cout<<a[0].volume()<<endl;
    return 0;
}

posted @ 2022-07-31 17:11  鹅城小铁匠  阅读(47)  评论(0)    收藏  举报
Fork me on GitHub