第五周作业(三)

来源:互联网 发布:淘宝网投资 编辑:程序博客网 时间:2024/05/13 12:32
  1. #include <iostream>  
  2. using namespace std;  
  3. class Box  
  4. {  
  5. public:  
  6.  Box();  
  7.  Box(int h,int w,int l):height(h),width(w),length(l){};  
  8.  int volume();  
  9.  int area();  
  10.  void input();  
  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 Box::area()  
  22. {  
  23.  int s;  
  24.  s=height*width+width*length+height*length;  
  25.  return (2*s);  
  26. }  
  27. Box::Box()  
  28. {  
  29.  height=10;  
  30.  width=10;  
  31.  length=10;  
  32. }  
  33. void Box::input()  
  34. {  
  35.  cin>>height>>width>>length;  
  36. }  
  37. int main()  
  38. {  
  39.  Box a[5]={  
  40.   Box(10,12,15),  
  41.   Box(15,18,20),  
  42.   Box(16,20,26),  
  43.   Box(),  
  44.   Box()  
  45.  };  
  46.     cout<<"长方柱1的面积是:"<<a[0].area()<<'\t'<<"体积是:"<<a[0].volume()<<endl<<endl;  
  47.     cout<<"长方柱2的面积是:"<<a[1].area()<<'\t'<<"体积是:"<<a[1].volume()<<endl<<endl;  
  48.     cout<<"长方柱3的面积是:"<<a[2].area()<<'\t'<<"体积是:"<<a[2].volume()<<endl<<endl;  
  49.     cout<<"长方柱4的面积是:"<<a[3].area()<<'\t'<<"体积是:"<<a[3].volume()<<endl<<endl;  
  50.     cout<<"请输入长方柱5的长,宽,高:";  
  51.     a[4].input();  
  52.     cout<<endl;  
  53.     cout<<"长方柱5的面积是:"<<a[4].area()<<'\t'<<"体积是:"<<a[4].volume()<<endl<<endl;  
  54.     return 0;  
  55. }