實驗報告 2

 

編寫基於對象的程序。數據成員包括長寬高,體積,要求用成員函數實現下面的功能

(1):由鍵盤輸入3個長方柱的長(length),寬(width),高(high)。

(2):計算長方柱的體積(volume)和表面積(areas)。

(3):輸入這3個長方柱的體積和表面積。

  1. #include <iostream>  
  2.   
  3. using namespace std;  
  4.   
  5. class Figure  
  6.   
  7. {  
  8. public:  
  9.     void set_data();  
  10.   
  11.     float volume();  
  12.   
  13.     float areas();                  
  14.   
  15.     void show_data();  
  16.   
  17. private:  
  18.     float len;  
  19.   
  20.     float weight;  
  21.   
  22.     float high;  
  23.   
  24.     float A;  
  25.   
  26.     float V;  
  27.   
  28. };  
  29.   
  30.  int main()  
  31.  {  
  32.   
  33.      Figure box1,box2,box3;  
  34.   
  35.      box1.set_data();      //第一組數據  
  36.   
  37.      box1.volume();  
  38.   
  39.      box1.areas();  
  40.   
  41.      box1.show_data();  
  42.   
  43.     box2.set_data();      //第二組數據  
  44.   
  45.      box2.volume();  
  46.   
  47.      box2.areas();  
  48.   
  49.      box2.show_data();  
  50.   
  51.      box3.set_data();     //第三組數據  
  52.   
  53.      box3.volume();  
  54.   
  55.      box3.areas();  
  56.   
  57.      box3.show_data();  
  58.   
  59.      return 0;  
  60.  }  
  61.   
  62.  void Figure::set_data()  
  63.  {  
  64.      cout<<"輸入對應的長、寬、高"<<endl;  
  65.   
  66.      cin>> len;  
  67.   
  68.      cin>> weight;  
  69.   
  70.      cin>> high;  
  71.  }  
  72.   
  73.  float Figure::areas()  
  74.   
  75.  {  
  76.   
  77.      return( 2* (len*weight+ len*high +high*weight));  
  78.   
  79.  }  
  80.   
  81.  float Figure::volume()  
  82.   
  83.  {  
  84.      return (len*weight*high);  
  85.  }  
  86.   
  87.  void Figure::show_data()  
  88.   
  89.  {  
  90.      cout<<"體積爲"<<volume()<<"表面積爲"<<areas()<<endl;  
  91.   
  92.  }  
發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章