多邊形周長計算(繼承


根據輸入的多邊形信息,相應建立一個多邊形類對象或矩形類對象或等邊多邊形類對象,計算每一個多邊形的周長並且輸出其邊數和周長。

  1 #include<iostream>
  2 #include<string>
  3 using namespace std;
  4 
  5 class polygon
  6 { 
  7 protected:
  8     int number;//邊數,最多不超過100條邊
  9 private:
 10     int side_length[100];//邊長數組
 11 public:
 12     polygon(int num):number(num){}
 13     polygon(){}//構造函數根據需要重載
 14     void set_side(int s,int k)//設置邊長
 15     {
 16         side_length[k]=s;
 17     }
 18     void set_number(int num)//設置邊數
 19     {
 20         number=num;
 21     }
 22     int perimeter();//計算多邊形周長
 23     void display();//輸出多邊形邊數和周長
 24 };
 25 
 26 int polygon::perimeter()
 27 {
 28     int sum(0);
 29     for(int i(0);i<number;i++)
 30     {
 31         sum+=side_length[i];//所有邊相加
 32     }
 33     return sum;
 34 }
 35 void polygon::display()
 36 {
 37     cout<<number<<' '<<perimeter()<<endl;//調用成員函數計算周長
 38 }
 39 
 40 
 41 class rectangle:public polygon//定義矩形類,繼承多邊形類
 42 {
 43 private:
 44     int height,width;
 45 public:
 46     rectangle(int h,int w,int n):polygon(n)//構造函數賦值
 47     {
 48         height=h,width=w,number=n;
 49     }
 50     int perimeter()//計算矩形的周長
 51     {
 52         int sum(0);
 53         sum=(height+width)*2;
 54         return sum;
 55     }
 56     void display()//輸出結果
 57     {
 58         cout<<number<<' '<<perimeter()<<endl;//調用成員函數
 59     }
 60 };
 61 
 62 class equal_polygon:public polygon//定義正多邊形類,繼承多邊形類
 63 {
 64 private:
 65     int side_len;
 66 public:
 67     equal_polygon(int s,int n):polygon(n)//構造函數賦值
 68     {
 69         number=n,side_len=s;
 70     }
 71     int perimeter()//計算周長
 72     {
 73         int sum(0);
 74         sum=side_len*number;
 75         return sum;
 76     }
 77     void display()//輸出結果
 78     {
 79         cout<<number<<' '<<perimeter()<<endl;
 80     }
 81 };
 82 
 83 int main()
 84 {    
 85     void ispolygon(),isrectangle(),isequal_polygon();//證明函數,在主函數后定義
 86     int a,i(0);
 87     cin>>a;//輸入a表示圖形個數
 88     do
 89     {
 90         int k;
 91         cin>>k;
 92         switch(k)
 93         {
 94         case 0:ispolygon();break;//多邊形執行ispolygon()
 95         case 1:isrectangle();break;//矩形
 96         case 2:isequal_polygon();break;//正多邊形
 97         }
 98     }
 99     while(i++,--a);//do{}while(i++,--a)等價於for(int i(0);i<a;i++)
100     return 0;
101 }
102 void ispolygon()
103 {
104     int a,i(0);
105     polygon poly;
106     while(cin>>a)
107     {
108         if(a!=-1) //輸入不為-1則繼續輸入
109         {
110             poly.set_side(a,i);//poly對象設置邊長
111             i++;
112         }
113         else
114             break;//輸入為-1則停止輸入
115     }
116     poly.set_number(i);//設置邊數
117     poly.display();//輸出結果
118 }
119 void isrectangle()
120 {
121     int a,b;
122     cin>>a>>b;
123     rectangle rect(a,b,4);//對象實體化類,賦初值
124     rect.display();//輸出結果
125 }
126 void isequal_polygon()
127 {
128     int s,n;
129     cin>>n>>s;
130     equal_polygon equa(s,n);//實體化類
131     equa.display();//輸出結果
132 }

 構造函數和析構函數只聲明不定義會報錯


免責聲明!

本站轉載的文章為個人學習借鑒使用,本站對版權不負任何法律責任。如果侵犯了您的隱私權益,請聯系本站郵箱yoyou2525@163.com刪除。



 
粵ICP備18138465號   © 2018-2025 CODEPRJ.COM