ep8_6.cpp
上传用户:wxcui2006
上传日期:2022-07-12
资源大小:1274k
文件大小:4k
源码类别:

书籍源码

开发平台:

Visual C++

  1. /*8.6 几何形体的派生关系如下:
  2.  
  3. 对平面形体有长和面积,对立体有表面积和体积,对几何图形基类,周长、面积和体积应怎样计算
  4. (用什么函数)?对平面圆形体积怎样计算(用什么函数|)?对立体圆形周长怎么计算(用什么函
  5. 数)?要求实现运行时的多态性。请编程,并测试。*/
  6. #include <iostream>
  7. #include <cmath>
  8. using namespace std;
  9. const double PI=3.1415926535;
  10. class Geometric_shape{//几何图形
  11. public:
  12. virtual double perimeter()=0; //周长
  13. virtual double area()=0; //面积
  14. virtual double volume()=0; //体积
  15. virtual void Show(){};
  16. };
  17. class Circle :public Geometric_shape{//圆
  18. double radius;
  19. public:
  20. Circle(){radius = 0; }
  21. Circle(double vv){radius = vv;}
  22. double perimeter(){return 2.0*PI*radius;} //周长
  23. double area(){return PI*radius*radius;} //面积
  24. double volume(){return 0;} //体积
  25. void Show(){cout<<"radius="<<radius<<endl;}
  26. };
  27. class Rectangle:public Geometric_shape{//矩形
  28. double width,length;
  29. public:
  30. Rectangle(){width=0; length=0;}//长宽
  31. Rectangle(double wid,double len){
  32. width = wid;
  33. length= len;
  34. }
  35. Rectangle(Rectangle& rr){
  36. width = rr.width;
  37. length = rr.length;
  38. }
  39. double perimeter(){return 2.0*(width+length);} //周长
  40. double area(){return width*length;} //面积
  41. double volume(){return 0;} //体积
  42. void Show(){cout<<"width="<<width<<'t'<<"length="<<length<<endl;}
  43. };
  44. class Triangle:public Geometric_shape{//三角形
  45. double a,b,c;
  46. public:
  47. Triangle(){a=0;b=0;c=0;}
  48. Triangle(double v1,double v2,double v3){a = v1;b = v2;c = v3;}
  49. double perimeter(){return a+b+c;} //周长
  50. double area(){
  51. double s=(a+b+c)/2.0;
  52. return sqrt(s*(s-a)*(s-b)*(s-c));
  53. } //面积
  54. double volume(){return 0;} //体积
  55. void Show(){cout<<"a="<<a<<'t'<<"b="<<b<<'t'<<"c="<<c<<endl;}
  56. };
  57. class Box:public Rectangle{//长方体
  58. double height;
  59. public:
  60. Box(){height=0;}
  61. Box(double wid,double len,double heg):Rectangle(wid,len){height=heg;}
  62. double volume(){return area()*height;} //体积
  63. };
  64. class Cylinder:public Circle {//圆柱体
  65. double height;
  66. public:
  67. Cylinder(){height=0;}
  68. Cylinder(double vv,double heg):Circle(vv){height=heg;}
  69. double volume(){return area()*height;} //体积
  70. };
  71. class Cone: public Circle {//圆锥
  72. double height;
  73. public:
  74. Cone(){height=0;}
  75. Cone(double vv,double heg):Circle(vv){height=heg;}
  76. double volume(){return area()*height/3;} //体积
  77. };
  78. class T_pyramid:public Triangle{//三棱锥
  79. double height;
  80. public:
  81. T_pyramid(){height=0;}
  82. T_pyramid(double v1,double v2,double v3,double heg):Triangle(v1,v2,v3){height=heg;}
  83. double volume(){return area()*height/3;} //体积
  84. };
  85. class T_prism:public Triangle{//三棱柱
  86. double height;
  87. public:
  88. T_prism(){height=0;}
  89. T_prism(double v1,double v2,double v3,double heg):Triangle(v1,v2,v3){height=heg;}
  90. double volume(){return area()*height;} //体积
  91. };
  92. int main(){
  93. Geometric_shape * gs;
  94. Circle cc1(10);
  95. Rectangle rt1(6,8);
  96. Triangle tg1(3,4,5);
  97. Box bx1(6,8,3);
  98. Cylinder cl1(10,3);
  99. Cone cn1(10,3);
  100. T_pyramid tpy1(3,4,5,3);
  101. T_prism tpr1(3,4,5,3);
  102. cc1.Show();//静态
  103. cout<<"圆周长:"<<cc1.perimeter()<<'t';
  104. cout<<"圆面积:"<<cc1.area()<<'t';
  105. cout<<"圆体积:"<<cc1.volume()<<endl;
  106. gs=&rt1;//动态
  107. gs->Show();
  108. cout<<"矩形周长:"<<gs->perimeter()<<'t';
  109. cout<<"矩形面积:"<<gs->area()<<'t';
  110. cout<<"矩形体积:"<<gs->volume()<<endl;
  111. gs=&tg1;//动态
  112. gs->Show();
  113. cout<<"三角形周长:"<<gs->perimeter()<<'t';
  114. cout<<"三角形面积:"<<gs->area()<<'t';
  115. cout<<"三角形体积:"<<gs->volume()<<endl;
  116. gs=&bx1;//动态
  117. gs->Show();
  118. cout<<"长方体底周长:"<<gs->perimeter()<<'t';
  119. cout<<"长方体底面积:"<<gs->area()<<'t';
  120. cout<<"长方体体积:"<<gs->volume()<<endl;
  121. gs=&cl1;//动态
  122. gs->Show();
  123. cout<<"圆柱体底周长:"<<gs->perimeter()<<'t';
  124. cout<<"圆柱体底面积:"<<gs->area()<<'t';
  125. cout<<"圆柱体体积:"<<gs->volume()<<endl;
  126. gs=&cn1;//动态
  127. gs->Show();
  128. cout<<"圆锥体底周长:"<<gs->perimeter()<<'t';
  129. cout<<"圆锥体底面积:"<<gs->area()<<'t';
  130. cout<<"圆锥体体积:"<<gs->volume()<<endl;
  131. gs=&tpy1;//动态
  132. gs->Show();
  133. cout<<"三棱锥底周长:"<<gs->perimeter()<<'t';
  134. cout<<"三棱锥底面积:"<<gs->area()<<'t';
  135. cout<<"三棱锥体积:"<<gs->volume()<<endl;
  136. gs=&tpr1;//动态
  137. gs->Show();
  138. cout<<"三棱柱底周长:"<<gs->perimeter()<<'t';
  139. cout<<"三棱柱底面积:"<<gs->area()<<'t';
  140. cout<<"三棱柱体积:"<<gs->volume()<<endl;
  141. return 0;
  142. }