问题:求圆柱体积
//面向过程
#include <iostream> using namespace std; int main() { double r, h, v; cin>>r>>h; v = 3.14 * r * r * h; cout<<v<<endl; return 0; }
//基于对象
#include <iostream> using namespace std; class Cup{ private: double radius; double height; public: Cup(double r, double h){ radius = r; height = h; } double getvolume(){ return 3.14 * radius * radius * height; } }; int main(){ double r, h; cin>>r>>h; Cup c(r, h); cout<<c.getvolume()<<endl; return 0; }
时间: 2024-10-10 02:53:52