类和对象1

需要求3个圆柱体的体积,请编一个基于对象的程序。数据成员包括radius(半径)、height(高),要求用成员函数实现以下功能:
1)由键盘分别输入3个圆柱体的半径和高;
2)计算圆柱体的体积;
3)输出3个圆柱体的体积。
#include <iostream>
using namespace std;
#define Pi 3.14
class cylinder//cylinder:圆柱体
{
    private:
        double radius;
        double heigth;
    public:
        double showVolume()
        {
            double Volume = Pi*radius*radius* heigth;//圆柱体体积=长*宽*高
            return Volume;
        }
    void setValue() 
    {
        cin >> radius >> heigth;
    }
};
int main() 
{
    cylinder R[3];
    int i; 
    for (i = 0; i < 3; i++) 
    {
        cout << "请输入第" << i+1 << "个圆柱体的半径和高" << endl;;
        R[i].setValue();
    }
    for (i = 0; i < 3; i++) 
    {
        cout <<"第"<<i+1<< "个圆柱体的体积为:";
        cout << R[i].showVolume() << endl;;
    }
    return 0;
}


 

#C/C++#
全部评论

相关推荐

点赞 收藏 评论
分享
牛客网
牛客企业服务