用c++求圆柱体体积
发布网友
发布时间:2023-07-06 08:35
我来回答
共2个回答
热心网友
时间:2024-11-16 03:44
#include<bits/stdc++.h>
using namespace std;
const double PI=3.1415926535;//圆周率
class Cylinder{
private:
double R,H;//底面半径,高
public:
Cylinder();//无参构造
Cylinder(double,double);//带参构造
double toVolume();//返回圆柱体体积
void print();//打印半径,高,圆柱体体积
};
Cylinder::Cylinder(){//无参构造
this->R=1;
this->H=10;
}
Cylinder::Cylinder(double R,double H){//带参构造
this->R=R;
this->H=H;
}
double Cylinder::toVolume(){//返回圆柱体体积
return pow(this->R,2)*PI*this->H;
}
void Cylinder::print(){//打印半径,高,圆柱体体积
cout<<"半径:";
cout<<this->R<<endl;
cout<<"高:";
cout<<this->H<<endl;
cout<<"体积:";
cout<<toVolume()<<endl;
}
int main(){
Cylinder *t1=new Cylinder();//无参构造,半径:默认1,高:默认10
cout<<t1->toVolume()<<endl;//输出体积
t1->print();// //打印半径,高,圆柱体体积
Cylinder *t2=new Cylinder(2.2,33.5);//带参构造
return 0;
}
热心网友
时间:2024-11-16 03:44
代码及其运行效果