項目三--基於對象的程序

/* 
* Copyright (c) 2011, 煙臺大學計算機學院 
* All rights reserved. 
* 作    者:王靜  
* 完成日期:2013  年  4  月  2  日 
* 版 本 號:v1.0 
* 輸入描述:

* 問題描述:

* 程序輸出:

* 問題分析:
* 算法設計:略 
*/  

#include<iostream>
#include<cmath>
using namespace std;
class Bulk
{private:
 double length;
 double width;
 double heigth;
 public:
 Bulk(double l=1,double w=1,double h=1);
 void show();
 double meter(void);//計算長方柱的體積
 double volume(void);//計算並返回長方柱的表面積
 void get_value();
};
Bulk::Bulk(double l,double w,double h)
{
        length=l;width=w;heigth=h;
}
void Bulk::get_value()
{
    cout<<"請輸入長方柱的長,寬,高"<<endl;
    cin>>length>>width>>heigth;
}
void Bulk::show()
{
    cout<<"長方柱的長爲"<<length<<"寬爲"<<width<<"高爲"<<heigth<<endl;
}
double Bulk::meter()
{
    double met;
    met=(length*width+length*heigth+width*heigth)*2;
    return met;
}
double Bulk::volume()
{
    double vol;
    vol=length*width*heigth;
    return vol;
}

int main()
{
    Bulk b[5]={Bulk(2.3,4.5,6.7),Bulk(1.5,3,4),Bulk(10,5)};
 b[4].get_value();

 b[0].show();
 cout<<"表面積爲"<<b[0].meter()<<endl;
 b[1].show();
 cout<<"體積爲"<<b[1].volume()<<endl;
 b[2].show();
 cout<<"表面積爲"<<b[2].meter()<<endl;
 b[3].show();
 cout<<"體積爲"<<b[3].volume()<<endl;
 b[4].show();
 cout<<"表面積爲"<<b[4].meter()<<endl;
 cout<<"體積爲"<<b[4].volume()<<endl;
  return 0;
}


 


運行結果:
(貼圖)

心得體會:


 

發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章