編寫基於對象的程序。數據成員包括長寬高,體積,要求用成員函數實現下面的功能
(1):由鍵盤輸入3個長方柱的長(length),寬(width),高(high)。
(2):計算長方柱的體積(volume)和表面積(areas)。
(3):輸入這3個長方柱的體積和表面積。
- #include <iostream>
- using namespace std;
- class Figure
- {
- public:
- void set_data();
- float volume();
- float areas();
- void show_data();
- private:
- float len;
- float weight;
- float high;
- float A;
- float V;
- };
- int main()
- {
- Figure box1,box2,box3;
- box1.set_data(); //第一組數據
- box1.volume();
- box1.areas();
- box1.show_data();
- box2.set_data(); //第二組數據
- box2.volume();
- box2.areas();
- box2.show_data();
- box3.set_data(); //第三組數據
- box3.volume();
- box3.areas();
- box3.show_data();
- return 0;
- }
- void Figure::set_data()
- {
- cout<<"輸入對應的長、寬、高"<<endl;
- cin>> len;
- cin>> weight;
- cin>> high;
- }
- float Figure::areas()
- {
- return( 2* (len*weight+ len*high +high*weight));
- }
- float Figure::volume()
- {
- return (len*weight*high);
- }
- void Figure::show_data()
- {
- cout<<"體積爲"<<volume()<<"表面積爲"<<areas()<<endl;
- }