#include<iostream> #include<cstdlib> #include<ctime> using namespace std; class box { public: box(double alength=1.0,double abreadth=1.0,double aheight=1.0); ~box() { cout<<endl<<"constructor destroyed."<<endl; } double getvolume()const { return height*length*breadth; } double getlength()const { return length; } double getbreadth()const { return breadth; } double getheight()const { return height; } bool operator<(const box& abox)const { return this->getvolume()<abox.getvolume(); } private: double length; double height; double breadth; }; box::box(double alength,double abreadth,double aheight) { length=alength; breadth=abreadth; height=aheight; } inline int random(int num) { return 1+(num*(rand()))/(RAND_MAX+1); } int main() { box boxes[20]; box* pbox=new box; pbox=boxes; const int dimlimit=100; srand((unsigned)time(0)); for(int i=0;i<20;i++) { boxes[i]=box(random(dimlimit),random(dimlimit),random(dimlimit)); cout<<endl<<"The "<<i+1<<"th box is:"<<boxes[i].getlength()<<" by "<<boxes[i].getbreadth() <<" by "<<boxes[i].getheight(); } box minibox; minibox=*pbox; for(int j=1;j<20;j++) { if(boxes[j]<minibox) minibox=boxes[j]; } cout<<endl<<"In the 20 boxes,the minimum box is: "<<minibox.getlength()<<" by "<<minibox.getbreadth() <<" by "<<minibox.getheight()<<endl; return 0; }

评论