#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(this->height*this->width*this->length);
}
int main()
{
Box a[3]={ //定义对象数组
Box(10,12,15),
Box(18,20),
Box(16,20,26)
};
for(int i=0;i<3;i++)//用循环调用a【0】,a【1】,a【2】.volume;
cout<<"volume of a["<<0<<"] is "<<a[i].volume()<<endl;
return 0;
}
对象数组的使用及this指针(自己改的,虽然只是一小点,但是还是有成就感)
![](https://img2024.cnblogs.com/blog/35695/202407/35695-20240713070336838-1837943664.jpg)