对象数组的使用方法
1 #include <iostream> 2 3 /* run this program using the console pauser or add your own getch, system("pause") or input loop */ 4 using namespace std; 5 class Box 6 { 7 public: 8 Box(int h=10,int w=12,int len=15):height(h),width(w),length(len){ 9 } 10 int volume(); 11 private: 12 int height; 13 int width; 14 int length; 15 }; 16 17 int Box::volume() 18 { 19 return(height*width*length); 20 } 21 int main(int argc, char** argv) { 22 Box a[3]={ 23 Box(10,12,15), 24 Box(15,18,20), 25 Box(16,20,26) 26 }; 27 cout<<"volume of a[0] is "<<a[0].volume()<<endl; 28 cout<<"volume of a[1] is "<<a[1].volume()<<endl; 29 cout<<"volume of a[2] is "<<a[2].volume()<<endl; 30 return 0; 31 }