- #include <iostream>
- #include <cmath>
- using namespace std;
- class Bulk
- {
- public:
- Bulk(double x=1.0,double y=1.0,double z=1.0):length(x),width(y),height(z) {};
- void get_value();
- double area();
- double volume();
- void show_message();
- private:
- double length;
- double width;
- double height;
- };
- void Bulk::get_value()
- { cout<<"请输入长宽高:"<<endl;
- cin>>length;
- cin>>width;
- cin>>height;
- }
- double Bulk::area()
- {
- return (2*(length*width+length*height+width*height));
- }
- double Bulk::volume()
- {
- return length*width*height;
- }
- void Bulk::show_message()
- {
- cout<<"长方柱的体积为:"<<volume()<<endl;
- cout<<"长方柱的表面积为:"<<area()<<endl;
- }
- int main()
- {
- Bulk b[5]= {Bulk(2.3,4.5,6.7),Bulk(1.5,3.4),Bulk(10.5)};
- b[4].get_value();
- int i;
- for(i=0; i<5; i++)
- {
- b[i].area();
- b[i].volume();
- b[i].show_message();
- }
- return 0;
- }
项目三 用对象数组操作长方柱
最新推荐文章于 2016-05-17 15:50:12 发布