当前位置: > c++:建立一个类cylinder,cylinder的构造函数被传递了两个double值,分别表示圆柱体的半径和高度,...
题目
c++:建立一个类cylinder,cylinder的构造函数被传递了两个double值,分别表示圆柱体的半径和高度,
用类cylinder 计算圆柱体的体积,并存储在一个double变量中,并在cylinder中包含一个成员函数vol(),来显示每个对象的体积.
1,建立一个静态成员 static int total_vol;累加体积
2,建立一个cylinder 的友元函数用来获取它的私有数据
3,main函数中建立三个类,cylinder的对象,并用静态成员完成累加,友元显示数据.
下面三点是附加要求,得一并写在程序中,麻烦你们了.

提问时间:2021-03-23

答案
#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
举一反三
我想写一篇关于奥巴马的演讲的文章,写哪一篇好呢?为什么好
奥巴马演讲不用看稿子.为什么中国领导演讲要看?
想找英语初三上学期的首字母填空练习……
英语翻译
1,人们染上烟瘾,最终因吸烟使自己丧命.
版权所有 CopyRight © 2012-2019 超级试练试题库 All Rights Reserved.