Guest User

Untitled

a guest
Feb 21st, 2018
81
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 3.29 KB | None | 0 0
  1. //---- 宣告類別 Shape -------------------------------
  2. class Shape
  3. {
  4. private:
  5. int Radius,Height;
  6. public:
  7. virtual void getRadius()=0;/*輸入半徑的值*/
  8. virtual void getHeight()=0;/*輸入高度的值*/
  9. //
  10. //照20-18頁的sample,完整的sample在20-19 ,及20-31
  11. //主框架20-19 搭配20-31
  12. //C的部份參考20-30
  13. //ab可以寫在一起 c
  14.  
  15. };
  16.  
  17.  
  18. //---- 宣告類別 Circle ------------------------------
  19. class Circle : public Shape
  20. {
  21. protected:
  22. int Radius,Height;
  23. public:
  24. // virtual void getRadius()=0;/*輸入半徑的值*/
  25. // virtual void getHeight()=0; /*輸入半徑的值*/
  26.  
  27. void getRadius(/*int Radius*/) /*輸入半徑的值*/
  28. {
  29. printf("請輸入半徑\n");
  30. scanf("%d",&Radius);
  31. printf("半徑是%d\n",Radius);
  32. }
  33.  
  34. void getHeight(/*int Height*/) /*輸入半徑的值*/
  35. {
  36. printf("請輸入高\n");
  37. scanf("%d",&Height);
  38. printf("高是%d\n",Height);
  39. }
  40.  
  41. };
  42.  
  43.  
  44.  
  45. //---- 宣告類別 Cylinder ----------------------------
  46. class Cylinder :public Circle
  47. {
  48. //private:
  49. // int Radius,Height;
  50. public:
  51. // void getRadius(); /*輸入半徑的值*/
  52. // void getHeight(); /*輸入高的值*/
  53. void getRadius(/*int Radius*/) /*輸入半徑的值*/
  54. {
  55. printf("請輸入半徑\n");
  56. scanf("%d",&Radius);
  57. printf("半徑是%d\n",Radius);
  58. }
  59.  
  60. void getHeight(/*int Height*/) /*輸入半徑的值*/
  61. {
  62. printf("請輸入高\n");
  63. scanf("%d",&Height);
  64. printf("高是%d\n",Height);
  65. }
  66.  
  67. double CVolume() /*計算圓柱體積 */
  68. {
  69. return 2*3.14*Radius*(Height+Radius);/*體積公式*/
  70. }
  71. double CArea() /*計算圓柱表面積*/
  72. {
  73. return Radius*Radius*3.14*Height;/*表面積公式*/
  74. }
  75. };
  76.  
  77.  
  78.  
  79. //---- 宣告類別 Sphere ----------------------------
  80. class Sphere :public Circle
  81. {
  82. private:
  83. int Radius;
  84. public:
  85. // void getRadius(); /*輸入半徑的值*/
  86. void getRadius(/*int Radius*/) /*輸入半徑的值*/
  87. {
  88. printf("請輸入半徑\n");
  89. scanf("%d",&Radius);
  90. printf("半徑是%d\n",Radius);
  91. }
  92.  
  93.  
  94. double Area()
  95. {
  96. return 4*3.14*Radius*Radius;/*球面積公式*/
  97. }
  98. double Volume()/*計算圓球體積 */
  99. {
  100. return (4*3.14*Radius*Radius*Radius)/3;/*球體積公式*/
  101. }
  102.  
  103. };
  104.  
  105.  
  106. #include <stdio.h>
  107. #include <stdlib.h>
  108. #include <math.h>
  109. #include "Shape.h"
  110. #include "Circle.h"
  111. #include "Cylinder.h"
  112. #include "Sphere.h"
  113. // ---- 主程式 --------------------------------------
  114. int main()
  115. {
  116. Shape *M;
  117. Circle T;
  118. Cylinder A;
  119. Sphere C;
  120.  
  121.  
  122. T.getRadius();
  123. T.getHeight();
  124.  
  125.  
  126.  
  127. // T.Radius=&A.Radius;
  128. // T.Height=&A.Heigt;
  129. M=&A;
  130. printf("圓柱面積是%5.2f\n",A.CArea());
  131. printf("圓柱體積是%5.2f\n",A.CVolume());
  132. // delete T;
  133. delete M;
  134.  
  135.  
  136. T.getRadius();
  137.  
  138. M=&C;
  139. printf("球面積是%5.2f\n",C.Area());
  140. printf("球體積是%5.2f\n",C.Volume());
  141. // delete T;
  142. delete M;
  143.  
  144.  
  145. system("pause");
  146. return 0;
  147. }
Add Comment
Please, Sign In to add comment