面向对象程序设计 第五次实验参考代码

[cpp]  view plain copy 在CODE上查看代码片 派生到我的代码片
  1. #include <iostream.h>    
  2. #include <math.h>    
  3.     
  4. const double PI=3.14;    
  5.     
  6. class Geometric_shape{    
  7. public:    
  8.     Geometric_shape()    
  9.     {    
  10.     }    
  11.     virtual double Get_Girth()=0;  //周长    
  12.      virtual double Get_Area()=0;   //面积    
  13.     virtual double Get_Volume()=0; //体积    
  14.     virtual double Get_SurArea()=0; //表面积    
  15.     virtual void print()=0;    
  16. };    
  17.     
  18. class Rectangle:public Geometric_shape{    
  19. protected:    
  20.     double length,width;    
  21. public:    
  22.     Rectangle(double l,double w):length(l),width(w){}    
  23.     double Get_Girth()    
  24.     {    
  25.         return 2 * (width + length);    
  26.     }    
  27.     double Get_Area()    
  28.     {    
  29.         return width * length;    
  30.     }    
  31.     virtual double Get_Volume()    
  32.     {    
  33.         return 0.0;    
  34.     }    
  35.     virtual double Get_SurArea()    
  36.     {    
  37.         return 0.0;    
  38.     }    
  39.     void print()    
  40.     {    
  41.         cout<<"The Rectangle's Girth is:"<<Get_Girth()<<endl;    
  42.         cout<<"The Rectangle's Area is:"<<Get_Area()<<endl;    
  43.     }    
  44. };    
  45.     
  46. class Circle:public Geometric_shape{    
  47. protected:    
  48.     double radius;    
  49. public:    
  50.     Circle(double l):Geometric_shape(),radius(l){}    
  51.     double Get_Girth()    
  52.     {    
  53.         return 2 * PI * radius;    
  54.     }    
  55.     double Get_Area()    
  56.     {    
  57.         return PI * radius * radius;    
  58.     }    
  59.     virtual double Get_Volume()    
  60.     {    
  61.         return 0.0;    
  62.     }    
  63.     virtual double Get_SurArea()    
  64.     {    
  65.         return 0.0;    
  66.     }    
  67.     void print()    
  68.     {    
  69.         cout<<"The Circle's Girth is:"<<Get_Girth()<<endl;    
  70.         cout<<"The Circle's Area is:"<<Get_Area()<<endl;    
  71.     }    
  72. };    
  73.     
  74. class Triangle:public Geometric_shape{    
  75. protected:    
  76.     double len1,len2,len3;    
  77. public:    
  78.     Triangle(double a,double b,double c)    
  79.     {    
  80.         len1 = a;    
  81.         len2 = b;    
  82.         len3 = c;    
  83.     }    
  84.     double Get_Girth()    
  85.     {    
  86.         return len1 + len2 + len3;    
  87.     }    
  88.     double Get_Area()    
  89.     {    
  90.         double p = Get_Girth() / 2;    
  91.         return sqrt(p * (p - len1) * (p - len2) * (p - len3));    
  92.     }    
  93.     virtual double Get_Volume()    
  94.     {    
  95.         return 0.0;    
  96.     }    
  97.     virtual double Get_SurArea()    
  98.     {    
  99.         return 0.0;    
  100.     }    
  101.     void print()    
  102.     {    
  103.         cout<<"The Triangle's Grith is:"<<Get_Girth()<<endl;    
  104.         cout<<"The Triangle's Area is:"<<Get_Area()<<endl;    
  105.     }    
  106. };    
  107.     
  108. class Box:public Rectangle{    
  109. protected:    
  110.     double height;    
  111. public:    
  112.     Box(double l,double w,double h):Rectangle(l,w),height(h){}    
  113.     double Get_SurArea()    
  114.     {    
  115.         return 2 * (length * width + width * height + height * length);    
  116.     }    
  117.     double Get_Volume()    
  118.     {    
  119.         return length * width * height;    
  120.     }    
  121.     void print()    
  122.     {    
  123.         cout<<"The Box's SurArea is:"<<Get_SurArea()<<endl;    
  124.         cout<<"The Box's volume is:"<<Get_Volume()<<endl;    
  125.     }    
  126. };    
  127.     
  128. class Column:public Circle{    
  129. private:    
  130.     double height;    
  131. public:    
  132.     Column(double l,double h):Circle(l),height(h){}    
  133.     double Get_SurArea()    
  134.     {    
  135.         double a;    
  136.         a = 2 * Get_Area() + 2 * PI * radius * height;    
  137.         return a;    
  138.     }    
  139.     double Get_Volume()    
  140.     {    
  141.         return Get_Area() * height;    
  142.     }    
  143.     void print()    
  144.     {    
  145.         cout<<"The Cylinder's SurArea is:"<<Get_SurArea()<<endl;    
  146.         cout<<"The Cylinder's Volume is:"<<Get_Volume()<<endl;    
  147.     }    
  148. };    
  149.     
  150. class Ball:public Circle{    
  151. public:    
  152.     Ball(double l):Circle(l){}    
  153.     double Get_SurArea()    
  154.     {    
  155.         return 4.0 * Get_Area();    
  156.     }    
  157.     double Get_Volume()    
  158.     {    
  159.         return 4.0 * Get_Area() * radius /3.0;    
  160.     }    
  161.     void print()    
  162.     {    
  163.         cout<<"The Cone's SurArea is:"<<Get_SurArea()<<endl;    
  164.         cout<<"The Cone's Volume is:"<<Get_Volume()<<endl;    
  165.     }    
  166. };    
  167.     
  168. void fun(Geometric_shape &p)    
  169. {    
  170.     p.print();    
  171. }    
  172.     
  173.     
  174. void main()    
  175. {    
  176.     Circle c(2.5);    
  177.     fun(c);    
  178.     Rectangle r(2.4,3.6);    
  179.     fun(c);    
  180.     Ball b(3.5);    
  181.     fun(b);    
  182.     Column co(3.5,5.3);    
  183.     fun(co);    
  184.     Triangle t(2.3,4.2,3.4);    
  185.     fun(t);    
  186.     Box bo(3,5.2,3.6);    
  187.     fun(bo);    
  188. }   
  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值