• 点类派生出圆类,圆类派生出圆柱类


    #include <iostream>
    #include <stdlib.h>
    using namespace std;
    const double PI=3.14159;

    //基类的定义
    class Point
    {
    private:
    int x,y;
    public:
    Point(int xx,int yy)
    {
    x=xx;
    y=yy;
    }
    int Getx()
    {
    return x;
    }
    int Gety()
    {
    return y;
    }
    /*
    void show()
    {
    cout<<x<<","<<y<<endl;
    }
    */
    friend ostream &operator<<(ostream &,const Point &);
    };
    ostream &operator<<(ostream &show,const Point &p)
    {
    show<<"("<<p.x<<","<<p.y<<")"<<endl;
    return show;
    }

    //定义子类“圆”,公共的继承
    class Circle:public Point
    {
    protected:
    int r;
    public:
    Circle(int xx,int yy,int rr):Point(xx,yy)
    {
    r=rr;
    }
    int Getr()
    {
    return r;
    }
    double circumferen() const
    {
    return 2*PI*r;
    }
    double area()
    {
    return PI*r*r;
    }
    friend ostream &operator<<(ostream &,const &);
    };

    ostream &operator<<(ostream &show.const Point &p)
    {
    show<<"("<<c.x<<","<<c.y<<")",半径"<<c.r<<",周长:"<<
    c.circmferen()<<"面积:"<<c.area()<<endl;
    return show;
    }
    class Cylinder:public Circle
    {
    private:
    int h;
    public:
    Cylinder(int xx=0,int yy=0,int rr=0,int hh=0):Circle(xx,yy,rr)
    {
    h=hh;
    }
    int Geth()
    {
    return h;
    }
    double Cy_area() const
    {
    return 2*Circle::area()+2*PI*r*h;
    }
    double volume()
    {
    return Circle::area()*h;
    }
    friend ostream &operator<<(ostream &,const Point &);
    };
    ostream &operator<<(ostream &show.const Point &cy)
    {
    show<<"("<<cy.x<<","<<cy.y<<")",半径"<<cy.r<<",表面积:"<<cy.Cy_area()
    <<"体积:"<<cy.volume()<<endl;
    return show;
    }

    int main()
    {
    Point objP(3,5);
    // cout<<"点类的对象:"<<objP;
    cout<<"横坐标的值:"<<objP.Getx()<<" ";
    cout<<"纵坐标的值:"<<objP.Gety()<<endl;

    Circle objC(3,5,9);
    // cout<<"圆类的对象:"<<objC;
    cout<<"圆的半径:"<<objC.Getr()<<endl;
    cout<<"圆的周长:"<<objC.circumferen()<<end;;
    cout<<"圆的面积:"<<objC.area()<<endl;

    Cylinder objCy(3,5,9,12);
    // cout<<"圆柱类的对象:"<<objCy;
    cout<<"圆柱的表面积:"<<objCy.Cy_area()<<end;;
    cout<<"圆柱的体积:"<<objC.volume()<<endl;

    return 1;
    }

  • 相关阅读:
    Geohash
    Go加密解密之RSA[转]
    在MACOS上实现交叉编译
    [转]MySQL与MongoDB的操作对比
    CentOS 6 使用 yum 安装MongoDB及服务器端配置
    Ubuntu下PHP的扩展
    golang 图片处理,剪切,base64数据转换,文件存储
    性能测试应用领域
    性能测试用例、策略和方法
    性能测试类型
  • 原文地址:https://www.cnblogs.com/duanqibo/p/11110349.html
Copyright © 2020-2023  润新知