实验报告

来源:互联网 发布:雅马哈yht 299 知乎 编辑:程序博客网 时间:2024/04/27 16:21
 
  1. #include <iostream>         
  2. using namespace std;      
  3. class Long      
  4. {      
  5. public:      
  6.     Long();     
  7.     Long(float x,float y,float z):length(x),width(y),heigth(z){};    
  8.     void show();      
  9.     void getvalue();      
  10. private:      
  11.     float length;      
  12.     float width;      
  13.     float heigth;      
  14.     float volume ;      
  15.     float surface ;      
  16. };      
  17. int main()      
  18. {      
  19.     int i;      
  20.     Long L[5]={Long(12,10,15),Long(13,14,15),Long(7,8,9)};//前边必须要声明带参数的构造函数才行,否则在这里对数组元素直接初始化将会出错。     
  21.     L[4].getvalue();    
  22.     for(i=0;i<5;i++)    
  23.     {    
  24.         L[i].show();    
  25.     }    
  26.     return 0;      
  27. }    
  28.     
  29.     
  30. Long::Long()     
  31. {     
  32.     heigth=10;     
  33.     width=10;     
  34.     length=10;     
  35. }     
  36.     
  37. void Long::show()      
  38. {      
  39.     cout<<"三边为:"<<length<<" "<<width<<" "<<heigth<<endl;      
  40.     volume=length*width*heigth;      
  41.     surface =2*(length*heigth+length*width+width*heigth);      
  42.     cout<<"体积为:"<<volume<<" "<<" 面积为:"<<surface<<endl;      
  43. }      
  44.     
  45. void Long::getvalue()      
  46. {      
  47.     float x,y,z;    
  48.     cout<<"请输入三边: ";    
  49.     cin>>x;     
  50.     cin>>y;    
  51.     cin>>z;    
  52.     length=x;      
  53.     width=y;      
  54.     heigth=z;    
  55. }    
原创粉丝点击