Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- //---- 宣告類別 Shape -------------------------------
- class Shape
- {
- private:
- int Radius,Height;
- public:
- virtual void getRadius()=0;/*輸入半徑的值*/
- virtual void getHeight()=0;/*輸入高度的值*/
- //
- //照20-18頁的sample,完整的sample在20-19 ,及20-31
- //主框架20-19 搭配20-31
- //C的部份參考20-30
- //ab可以寫在一起 c
- };
- //---- 宣告類別 Circle ------------------------------
- class Circle : public Shape
- {
- protected:
- int Radius,Height;
- public:
- // virtual void getRadius()=0;/*輸入半徑的值*/
- // virtual void getHeight()=0; /*輸入半徑的值*/
- void getRadius(/*int Radius*/) /*輸入半徑的值*/
- {
- printf("請輸入半徑\n");
- scanf("%d",&Radius);
- printf("半徑是%d\n",Radius);
- }
- void getHeight(/*int Height*/) /*輸入半徑的值*/
- {
- printf("請輸入高\n");
- scanf("%d",&Height);
- printf("高是%d\n",Height);
- }
- };
- //---- 宣告類別 Cylinder ----------------------------
- class Cylinder :public Circle
- {
- //private:
- // int Radius,Height;
- public:
- // void getRadius(); /*輸入半徑的值*/
- // void getHeight(); /*輸入高的值*/
- void getRadius(/*int Radius*/) /*輸入半徑的值*/
- {
- printf("請輸入半徑\n");
- scanf("%d",&Radius);
- printf("半徑是%d\n",Radius);
- }
- void getHeight(/*int Height*/) /*輸入半徑的值*/
- {
- printf("請輸入高\n");
- scanf("%d",&Height);
- printf("高是%d\n",Height);
- }
- double CVolume() /*計算圓柱體積 */
- {
- return 2*3.14*Radius*(Height+Radius);/*體積公式*/
- }
- double CArea() /*計算圓柱表面積*/
- {
- return Radius*Radius*3.14*Height;/*表面積公式*/
- }
- };
- //---- 宣告類別 Sphere ----------------------------
- class Sphere :public Circle
- {
- private:
- int Radius;
- public:
- // void getRadius(); /*輸入半徑的值*/
- void getRadius(/*int Radius*/) /*輸入半徑的值*/
- {
- printf("請輸入半徑\n");
- scanf("%d",&Radius);
- printf("半徑是%d\n",Radius);
- }
- double Area()
- {
- return 4*3.14*Radius*Radius;/*球面積公式*/
- }
- double Volume()/*計算圓球體積 */
- {
- return (4*3.14*Radius*Radius*Radius)/3;/*球體積公式*/
- }
- };
- #include <stdio.h>
- #include <stdlib.h>
- #include <math.h>
- #include "Shape.h"
- #include "Circle.h"
- #include "Cylinder.h"
- #include "Sphere.h"
- // ---- 主程式 --------------------------------------
- int main()
- {
- Shape *M;
- Circle T;
- Cylinder A;
- Sphere C;
- T.getRadius();
- T.getHeight();
- // T.Radius=&A.Radius;
- // T.Height=&A.Heigt;
- M=&A;
- printf("圓柱面積是%5.2f\n",A.CArea());
- printf("圓柱體積是%5.2f\n",A.CVolume());
- // delete T;
- delete M;
- T.getRadius();
- M=&C;
- printf("球面積是%5.2f\n",C.Area());
- printf("球體積是%5.2f\n",C.Volume());
- // delete T;
- delete M;
- system("pause");
- return 0;
- }
Add Comment
Please, Sign In to add comment