zoukankan      html  css  js  c++  java
  • 点类派生出圆类,圆类派生出圆柱类

    #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;
    }

  • 相关阅读:
    C++ 安全字符串拼接
    C code 字符串与整数的相互转化
    深入解析:分布式系统的事务处理经典问题及模型
    .NET分布式事务处理总结【下】
    用csc命令行手动编译cs文件
    委托和事件
    C#中的lock关键字
    SQL索引详解
    Quartz.NET 入门
    使用Topshelf创建Windows服务
  • 原文地址:https://www.cnblogs.com/duanqibo/p/11110349.html
Copyright © 2011-2022 走看看