zoukankan      html  css  js  c++  java
  • p243_5(3)

    源程序:

    #include <iostream>
    #include <cmath>
    #define PI 3.1415926
    using namespace std;

    class Geometry
    {
    public:
    Geometry() {};
    ~Geometry() {};
    virtual double Get_Area()
    {
    return 0.0;
    }
    virtual double Get_Perimeter()
    {
    return 0.0;
    }
    };
    //子类:圆
    class Circle :public Geometry
    {
    protected:
    double radius;
    public:
    Circle() {};
    Circle(double r)
    {
    radius = r;
    }
    ~Circle() {};
    double Get_Area()
    {
    return PI*radius*radius;
    }
    double Get_Perimeter()
    {
    return 2 * PI*radius;
    }
    };

    //圆的子类:球
    class Ball :public Circle
    {
    public:
    Ball(double rr) :Circle(rr)
    {
    }
    double Get_Area()
    {
    return 4 * PI * radius * radius ;
    }
    };

    //圆的子类:圆柱
    class Column :public Circle
    {
    private:
    double h;
    public:
    Column(double rr,double hh) :Circle(rr)
    {
    //r = rr;
    h = hh;
    }
    double Get_Area()
    {
    return 2 * PI * radius * radius + 2 * PI * radius * h;
    }

    double Get_Volume()
    {
    return PI * radius * radius * h;
    }
    };


    //子类:正方形
    class Square :public Geometry
    {
    private:
    double side;
    public:
    Square(double a)
    {
    side = a;
    }
    ~Square() {};
    double Get_Area()
    {
    return side*side;
    }
    double Get_Perimeter()
    {
    return 4*side;
    }
    };

    //子类:三角形
    class Triangle :public Geometry
    {
    private:
    double a,b,c;
    public:
    Triangle(double aa,double bb,double cc)
    {
    a = aa;
    b = bb;
    c = cc;
    }
    ~Triangle() {};
    double Get_Area()
    {
    int dd = (a + b + c) / 2;
    return sqrt(dd*(dd-a)*(dd-b)*(dd-c));
    }
    double Get_Perimeter()
    {
    return a+b+c;
    }
    };

    int main()
    {
    Geometry *p[3];
    p[0] = new Circle(5.5);
    p[1] = new Square(6.6);
    p[2] = new Triangle(3.0,4.0,5.0);

    cout<<"圆的面积为:"<<p[0]->Get_Area()<<endl;
    cout << "圆的周长为:"<< p[0]->Get_Perimeter() << endl;

    cout << "正方形的面积为:" <<p[1]->Get_Area() << endl;
    cout << "正方形的周长为:" <<p[1]->Get_Perimeter() << endl;

    cout << "三角形的面积为:" << p[2]->Get_Area() << endl;
    cout << "三角形的周长为:" << p[2]->Get_Perimeter() << endl;

    system("pause");
    return 1;

    }

    运行结果:

  • 相关阅读:
    jsp mysql 实现客户端简单分页查询
    jsp mysql 实现客户端简单数据的修改和删除
    jsp 简单把数据库数据,展示在网页
    XML当做数据库,完成增删查
    xml的增删查 dom的增改查 复杂注释
    修改目录下所有文件时间
    打开调试模式
    强化学习笔记4:无模型预测 model-free prediction
    强化学习笔记6:值函数估计Value function Approximation
    Declarative Pipeline语法介绍
  • 原文地址:https://www.cnblogs.com/duanqibo/p/12283538.html
Copyright © 2011-2022 走看看