#include<iostream> using namespace std; class Box { public: Box(double lengthvalue=1.0,double breadthvalue=1.0,double heightvalue=1.0); double volume(); int comparevolume(Box& otherbox); private: double length; double breadth; double height; }; Box::Box(double lengthvalue,double breadthvalue,double heightvalue):length(lengthvalue), breadth(breadthvalue),height(heightvalue) { cout<<endl <<"constructor called" <<endl; length=lengthvalue; breadth=breadthvalue; height=heightvalue; } double Box::volume() { return length*height*breadth; } int Box::comparevolume(Box& otherbox) { if(otherbox.volume()>this->volume()) return 0; else return 1; } int main() { Box firstbox(1.0,2.0,3.0); Box secondbox; Box* pthirdbox=new Box(0.1,2.5,3.5); cout<<endl<<"volume of first box is:" <<firstbox.volume()<<endl; cout<<"volume of second box is:" <<secondbox.volume()<<endl; cout<<"volume of third box is:" <<pthirdbox->volume()<<endl; if(firstbox.comparevolume(secondbox)) { if(firstbox.comparevolume(*pthirdbox)) { if(secondbox.comparevolume(*pthirdbox)) { cout<<endl <<"the largest_volume box is the first box." <<endl; cout<<"the second_volume box is the second box." <<endl; cout<<"the third_volume box is the the third box." <<endl; } else { cout<<endl <<"the largest_volume box is the first box." <<endl; cout<<"the second_volume box is the third box." <<endl; cout<<"the third_volume box is the the second box." <<endl; } } else { if(pthirdbox->comparevolume(secondbox)) { cout<<"the largest_volume box is the third box."<<endl; cout<<"the second_volume box is the first box."<<endl; cout<<"the third_volume box is the second box."<<endl; } else { cout<<"the largest_volume box is the third box."<<endl; cout<<"the second_volume box is the second box."<<endl; cout<<"the third_volume box is the first box."<<endl; } } } else { if(firstbox.comparevolume(*pthirdbox)) { cout<<endl <<"the largest_volume box is the second box." <<endl; cout<<"the second_volume box is the first box." <<endl; cout<<"the third_volume box is the the third box." <<endl; } else { if(pthirdbox->comparevolume(secondbox)) { cout<<"the largest_volume box is the third box."<<endl; cout<<"the second_volume box is the second box."<<endl; cout<<"the third_volume box is the first box."<<endl; } else { cout<<"the largest_volume box is the second box."<<endl; cout<<"the second_volume box is the third box."<<endl; cout<<"the third_volume box is the first box."<<endl; } } } return 0; }

评论