用c++计算圆柱体的体积,底圆的面积和周长

#include

#include

 

#define PI 3.1415926

 

using namespace std;

 

class Cylinder

{

 private:

  float r,h;

 public:

  Cylinder(float,float);

  float perimeter(); //底圆周长 

  float circleArea(); //底圆面积 

  float volume(); //圆柱体体积 

  float getH();

};

 

Cylinder::Cylinder(float num1,float num2)

{

 r=num1;

 h=num2;

}

 

float Cylinder::perimeter()

{

 return 2*PI*r;

}

 

float Cylinder::circleArea()

{

 return PI*pow(r,2);

}

 

float Cylinder::volume()

{

 return circleArea()*h;

}

 

float Cylinder::getH()

{

 return h;

}

 

//计算圆柱体表面积 

float cylinderArea(float r,float h)

{

 Cylinder cy=Cylinder(r,h);

 float perimeter=cy.perimeter(); //底圆周长 

 float circleArea=cy.circleArea(); //底圆面积

 float output;

 output=circleArea*2;

 output+=perimeter*cy.getH();

 return output; 

}

 

//计算圆柱体体积 

float volume(float r,float h)

{

 Cylinder cy=Cylinder(r,h);

 return cy.volume();

}

 

int main()

{

 Cylinder cy=Cylinder(1,10);

 cout<<"底圆周长:"<

  <<"\t圆柱体体积"<

 cout<<"================="<

 cout<<"cylinderArea(float r,float h):"<

  <<"\tvolume(float r,float h):"<

}

你可能感兴趣的:(c++)