//对象数组的简单例子 //注意对象数组定义时符号 #include<iostream> using namespace std; //类创建 class Box { public: //定义构造函数 Box(int h=2,int w=4,int l=8,int n=0):height(h),width(w),length(l),num(n){} //定义析构函数 ~Box() { cout<<num<<endl; } int volume(); private: int height; int width; int length; int num; }; //定义函数 int Box::volume() { return(height*width*length); } int main() { //注意对象数组定义时的符号 Box box[3]= { Box(23,34,45,1), Box(45,66,55,2), Box(55,77,89,3) }; int num=0; cout<<box[0].volume()<<"第"<<++num<<"次"<<endl; cout<<box[1].volume()<<"第"<<++num<<"次"<<endl; cout<<box[2].volume()<<"第"<<++num<<"次"<<endl; return 0; }