zoukankan      html  css  js  c++  java
  • 自考新教材-p255

    使用多态处理图形示例

    源程序:

    #include <iostream>

    #include <cmath>

    using namespace std;

    class CShape

    {

    protected:

      double acreage;

    public:

      CShape()

       {

        //cout<<"基类构造函数"<<endl;

      };

       virtual ~CShape()

       {

        //cout<<"基类析构函数"<<endl;

      }

      virtual double CalAcr()

      {

        return 0;

       };

       virtual void setAcreage(double acre){};

       virtual void PrintInfo(){};

    };

    class CRectangle:public CShape

    {

       double width,high;

    public:

       CRectangle(double w,double h)

      {

         //cout<<"矩形带参构造函数"<<endl;

         width = w;

         high = h;

      };

      CRectangle()

      {

        //cout<<"矩形无参构造函数"<<endl;

        width = 0;

         high = 0;

       };

       ~CRectangle()

      {

        //cout<<"矩形析构函数"<<endl;

      }

      virtual double CalAcr();

      virtual void PrintInfo();

      virtual void setAcreage(double);

    };

    class CCircle:public CShape

    {

      double radius;

    public:

      CCircle(double r)

      {

      //cout<<"圆形带参构造函数"<<endl;

      radius = r;

      };

      CCircle()

      {

        //cout<<"圆形无参构造函数"<<endl;

        radius = 0;

      };

      ~CCircle()

      {

        //cout<<"圆形析构函数"<<endl;

      }

      virtual double CalAcr();

      virtual void PrintInfo();

      virtual void setAcreage(double);

    };

    class CTriangle:public CShape

    {

      double a,b,c;

    public:

      CTriangle(double a,double b,double c)

       {

        //cout<<"三角形带参构造函数"<<endl;

        this->a=a;

        this->b=b;

        this->c=c;

      };

      CTriangle()

      {

        //cout<<"三角形无参构造函数"<<endl;

        a=0;

        b=0;

        c=0;

      };

      ~CTriangle()

       {

        //cout<<"三角形析构函数"<<endl;

      };

       virtual double CalAcr();

       virtual void PrintInfo();

      virtual void setAcreage(double);

    };

    double CRectangle::CalAcr()

    {

       return width*high;

    }

    void CRectangle::PrintInfo()

    {

       cout<<"矩形. 宽度="<<this->width<<",高度 ="<<this->high<<", 面积"<<this->acreage<<endl;

    }

    void CRectangle::setAcreage(double acre)

    {

      acreage = acre;

    }

     

    double CCircle::CalAcr()

    {

      return 3.14*radius*radius;

    }

    void CCircle::PrintInfo()

    {

      cout<<"圆. 半径="<<this->radius<<",面积"<<this->acreage<<endl;

    }

    void CCircle::setAcreage(double acre)

    {

      acreage = acre;

    }

     

    double CTriangle::CalAcr()

    {

      double p = (a+b+c)/2.0;

      return sqrt(p*(p-a)*(p-b)*(p-c));

    }

    void CTriangle::PrintInfo()

    {

      cout<<"三角形.三条边分别是: "<<this->a<<","<<this->b<<","<<this->c<<",面积="<<this->acreage<<endl;

    }

    void CTriangle::setAcreage(double acre)

    {

      acreage = acre;

    }

    CShape * pShapes[100];

     

    int main()

    {

      int i,n;

       double temp1,temp2,temp3;

       CRectangle *pr;CCircle *pc;CTriangle *pt;

       cin>>n;

       for(i=0;i<n;++i)

       {

        char c;

         cin>>c;

        switch(c)

         {

        case'R':case'r':

          cin>>temp1>>temp2;

          pr=new CRectangle(temp1,temp2);

          pr->setAcreage(pr->CalAcr());

          pShapes[i]=pr;

          break;

        case'C':case'c':

          cin>>temp1;

          pc=new CCircle(temp1);

          pc->setAcreage(pc->CalAcr());

          pShapes[i]=pc;

           break;

        case'T':case't':

          cin>>temp1>>temp2>>temp3;

          pt=new CTriangle(temp1,temp2,temp3);

          pt->setAcreage(pt->CalAcr());

          pShapes[i]=pt;

          break;

        }

      }

      if(n==1)

        cout<<"共有 "<<n<<"种图形,它是"<<endl;

      else

        cout<<"共有 "<<n<<"种图形,分别是"<<endl;

      for(i=0;i<n;++i)

       {

        //cout<<"1"<<endl;

        pShapes[i]->PrintInfo();

        delete pShapes[i];

       }

       system("pause");

      return 0;

    }

    运行结果:

  • 相关阅读:
    Android应用安全之外部动态加载DEX文件风险
    Android应用安全之Android APP通用型拒绝服务漏洞
    利用Cydia Substrate进行Android HOOK(二)
    Apk去签名校验详解
    nginx部署前端项目的一些配置【刚入门】
    java 日期格式化 将String日期重新格式化成String型【转】
    idea导入maven项目时需要注意
    element 'dependency' cannot have character [children], because the type's content type is element-only.
    腾讯云centos7执行 systemctl status postfix.service 启动postfix服务时出现 /usr/sbin/postconf: fatal: parameter inet_interfaces: no local interface found for ::1
    redis.clients.jedis.exceptions.JedisExhaustedPoolException: Could not get a resource since the pool is exhausted
  • 原文地址:https://www.cnblogs.com/duanqibo/p/12183280.html
Copyright © 2011-2022 走看看