作业帮 > 综合 > 作业

c++:建立一个类cylinder,cylinder的构造函数被传递了两个double值,分别表示圆柱体的半径和高度,

来源:学生作业帮 编辑:搜搜考试网作业帮 分类:综合作业 时间:2024/06/06 09:18:49
c++:建立一个类cylinder,cylinder的构造函数被传递了两个double值,分别表示圆柱体的半径和高度,
用类cylinder 计算圆柱体的体积,并存储在一个double变量中,并在cylinder中包含一个成员函数vol(),来显示每个对象的体积.
1,建立一个静态成员 static int total_vol;累加体积
2,建立一个cylinder 的友元函数用来获取它的私有数据
3,main函数中建立三个类,cylinder的对象,并用静态成员完成累加,友元显示数据.
下面三点是附加要求,得一并写在程序中,麻烦你们了.
c++:建立一个类cylinder,cylinder的构造函数被传递了两个double值,分别表示圆柱体的半径和高度,
#include
using namespace std;
#define PI 3.14159
class Cylinder{
public:
Cylinder(double radius,double height):radius(radius),height(height),volume(PI*radius*radius*height){
total_vol += volume;
}
double vol()const;
friend void getParas(double& radius,double&height,double&vol,double& total_vol,const Cylinder& obj);
private:
double radius; //radius
double height; //height
double volume;//volume of current object
static double total_vol;//total volume
};
double Cylinder::total_vol = 0;
double Cylinder::vol()const{
return this->volume;
}
void getParas(double& radius,double& height,double& vol,double& total_vol,const Cylinder& obj){ //get private member of Cylinder
radius = obj.radius;
height = obj.height;
vol = obj.volume;
total_vol = obj.total_vol;
}
int main(){
double rd = 0,hg = 0,vol = 0,total_vol = 0;
Cylinder cld_1(2,3.5),cld_2(5.2,7),cld_3(3.9,4.0);//3 objects
getParas(rd,hg,vol,total_vol,cld_3);
cout