最新文章:

首页 C/C++

C++由圆派生球,圆柱,圆锥

发布时间:2016年06月23日 评论数:抢沙发 阅读数:388

    设计的一个基类Circle类,再分别派生出圆球类Ball,圆锥类Taper 圆柱类Cylinder使用相关编程题方法求出圆的面积,其他几何图形的表面积和体积


    #include <iostream>
    #include <cmath>
    using namespace std;
    const long double PI = 3.1415926;
    class Circle
    {
        public:
            virtual long double area() = 0;//获得表面积
            virtual long double volume() = 0;//获得体积
    };
    //球
    class Ball:public Circle
    {
        public:
            Ball(long double radius):m_radius(radius){}
            long double volume(){
                return 4.0/3.0 * PI * m_radius * m_radius * m_radius;
            }
            long double area(){
                return 4 * PI * m_radius * m_radius;
            }
        private:
            long double m_radius;        //球的半径
    };
    //圆柱
    class Cylinder:public Circle{
        public:
            Cylinder(long double radius, long double high):m_radius(radius),m_high(high){}
            long double volume(){
                return PI * m_radius * m_radius * m_high;
            }
            long double area(){
                return PI * m_radius * m_radius + 2.0 * PI * m_radius * m_high;
            }
        private:
            long double m_radius;   //圆柱的半径
            long double m_high;     //圆柱的高
    };
    //圆锥
    class Taper :public Circle
    {
        public:
            Taper (long double radius, long double high):m_radius(radius),m_high(high){
                this->m_coneElement = sqrt(m_high * m_high + m_radius * m_radius);
            }
            long double volume(){
                return 1.0/3.0 * PI * m_radius * m_radius * m_high;
            }
            long double area(){
                return PI * m_radius * (m_radius + m_coneElement);
            }
        private:
            long double m_radius;//圆锥的半径
            long double m_high;  //圆锥的高
            long double m_coneElement;  //圆锥的母线长
    
    };
    
    int main()
    {
        Circle *p = NULL;
        p = new Taper (3,4);
        cout << "圆锥(3,4)的表面积是:" << p->area() << " 体积是:" << p->volume() << endl;
        delete p;
        p = NULL;
        //
        p = new Cylinder(6,4);
        cout << "圆柱(6,4)的表面积是:" << p->area() << " 体积是:" << p->volume() << endl;
        delete p;
        p = NULL;
        //
        p = new Ball(6);
        cout << "球(6)的表面积是:" << p->area() << " 体积是:" << p->volume() << endl;
        delete p;
        p = NULL;
        //
        return 0;
    }


二维码加载中...
本文作者:HDC      文章标题: C++由圆派生球,圆柱,圆锥
本文地址:http://hdcin.cn/?post=75
版权声明:若无注明,本文皆为“小胖Blog's”原创,转载请保留文章出处。
挤眼 亲亲 咆哮 开心 想想 可怜 糗大了 委屈 哈哈 小声点 右哼哼 左哼哼 疑问 坏笑 赚钱啦 悲伤 耍酷 勾引 厉害 握手 耶 嘻嘻 害羞 鼓掌 馋嘴 抓狂 抱抱 围观 威武 给力
提交评论

清空信息
关闭评论