长方柱

来源:互联网 发布:mysql 怎么查询密码 编辑:程序博客网 时间:2024/04/27 14:31
#include <iostream>  #include <cmath>  using namespace std;    class Volume  {  public:      void setdata(double length,double width,double height);      double area();      double volume();    private:      double length;      double width;      double height;  };  //下面定义需要的成员函数  void Volume::setdata(double l,double w,double h)//赋值  {      length=l;      width=w;      height=h;  }  double Volume::area()//求面积  {      return 2*(length*width+width*height+height*length);  }  double Volume::volume()//求体积  {      return length*width*height;  }    int main()  {      int t=3,i=1;      while(t--)      {          double l,w,h;          cout<<"请输入长方柱"<<i<<"的长: ";          cin>>l;          cout<<"请输入长方柱"<<i<<"的宽: ";          cin>>w;          cout<<"请输入长方柱"<<i<<"的高: ";          cin>>h;          Volume v;          v.setdata(l,w,h);          cout<<"长方柱"<<i<<"的表面积为: "<<v.area()<<endl;          cout<<"长方柱"<<i<<"的体积为: "<<v.volume()<<endl;          i++;      }        return 0;  }  

0 0
原创粉丝点击