第五次实验代码(其他班)——继承多态

  1. /**********************************************************/       
  2.   //Function                                :   main,Geometric及其派生类    
  3.   //parm                                    :         
  4.   //comment                                 :          
  5.   //return                                  :   void      
  6.   //Author                                  :          
  7.   //date                                    :   2013.1.4      
  8. /**********************************************************/    
  9. #include <iostream.h>  
  10. #include <math.h>  
  11.   
  12. const double PI=3.14159;  
  13.   
  14. class Geometric{  
  15. public:  
  16.     Geometric()  
  17.     {}  
  18.     virtual double Get_Girth()=0;  //周长  
  19.     virtual double Get_Area()=0;   //面积  
  20.     virtual double Get_Volume()=0; //体积  
  21.     virtual void print()=0;        //输出  
  22. };  
  23.   
  24. class Rectangle:public Geometric{  
  25. protected:  
  26.     double length,width;  
  27. public:  
  28.     Rectangle(double l,double w):Geometric(),length(l),width(w){}  
  29.     double Get_Girth()  
  30.     {  
  31.         return 2 * (width+length);  
  32.     }  
  33.     double Get_Area()  
  34.     {  
  35.         return width * length;  
  36.     }  
  37.     double Get_Volume()  
  38.     {  
  39.         return 0.0;  
  40.     }  
  41.     void print()  
  42.     {  
  43.         cout<<"The Rectangle's Girth is:"<<Get_Girth()<<endl;  
  44.         cout<<"The Rectangle's Area is:"<<Get_Area()<<endl;  
  45.     }  
  46. };  
  47.   
  48. class Circle:public Geometric{  
  49. protected:  
  50.     double radius;  
  51. public:  
  52.     Circle(double r):Geometric()  
  53.     {  
  54.         radius = r;  
  55.     }  
  56.     double Get_Girth()  
  57.     {  
  58.         return 2 * PI * radius;  
  59.     }  
  60.     double Get_Area()  
  61.     {  
  62.         return PI * radius * radius;  
  63.     }  
  64.     double Get_Volume()  
  65.     {  
  66.         return 0.0;  
  67.     }  
  68.     void print()  
  69.     {  
  70.         cout<<"The Circle's Girth is:"<<Get_Girth()<<endl;  
  71.         cout<<"The Circle's Area is:"<<Get_Area()<<endl;  
  72.     }  
  73. };  
  74.   
  75. class Ball:public Circle{  
  76. public:  
  77.     Ball(double r):Circle(r)  
  78.     {}  
  79.     double Get_Girth()  
  80.     {  
  81.         return 0;  
  82.     }  
  83.     double Get_Area()  
  84.     {  
  85.         return 4 * Circle::Get_Area();  
  86.     }  
  87.     virtual double Get_Volume()  
  88.     {  
  89.         return 4.0 / 3 * Get_Area() * radius;  
  90.     }  
  91.     void print()  
  92.     {  
  93.         cout<<"The Ball's Area is:"<<Get_Area()<<endl;  
  94.         cout<<"The Ball's volume is:"<<Get_Volume()<<endl;  
  95.     }  
  96. };  
  97.   
  98. class Column:public Circle{  
  99. protected:  
  100.     double height;  
  101. public:  
  102.     Column(double r,double h):Circle(r),height(h){}  
  103.     double Get_Area()  
  104.     {  
  105.         return Circle::Get_Girth() * height + 2 * Circle::Get_Area();  
  106.     }  
  107.     double Get_Volume()  
  108.     {  
  109.         return Circle::Get_Area() * height;  
  110.     }  
  111.     void print()  
  112.     {  
  113.         cout<<"The Column's Area is:"<<Get_Area()<<endl;  
  114.         cout<<"The Column's volume is:"<<Get_Volume()<<endl;  
  115.     }  
  116. };  
  117.   
  118. void fun(Geometric &p)  
  119. {  
  120.     p.print();  
  121. }  
  122.   
  123. void main()  
  124. {  
  125.     double r,h,w,l;  
  126.     cout<<"Please input the radius and height:"<<endl;  
  127.     cin>>r>>h;  
  128.     Circle obj_Cir(r);  
  129.     fun(obj_Cir);  
  130.     Ball obj_Bal(r);  
  131.     fun(obj_Bal);  
  132.     Column obj_Col(r,h);  
  133.     fun(obj_Col);  
  134.     cout<<"Please input the length and width:"<<endl;  
  135.     cin>>l>>w;  
  136.     Rectangle obj_Rec(l,w);  
  137.     fun(obj_Rec);  
  138. }  
  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 1
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论 1
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值