C++用抽象類別,衍生類別求圓,正方形,矩形,梯形,三角形面積

來源:互聯網
上載者:User

題目:

 寫一個程式,定義抽象基類Shape,由它派生出5個衍生類別:Cricle(圓形 , Square(正方形 , Rectangle (矩形),  Trapezoid (梯形),  Triangle(三角形).用虛函數分別計算幾重圖形面積,並求它們的和.要求用基類指標數組,使它的每一個元素指向一個衍生類別對象.

希望朋友們能幫我改進程式哦!!~

程式:

#include <iostream>
#include <math.h>
using namespace std;

class shape                    /*抽象基類*/
{
public:
 virtual float area()const {return 0;}
    virtual void shapename() const=0;
 virtual void show() const=0;
};

 

class circle:public shape               /*衍生類別圓*/
{
private: float r;
   float x,y;
public:circle(){r=0;x=0;y=0;}
    circle(float a,float b,float c){r=a;x=b;y=c;}
    float area()  const
    {
         return (3.14*r*r);
    }
    void shapename() const
    {
         
    cout<<"shapename:circle"<<endl;

    }
    void show() const                        /*輸出圓資訊*/
    {
         shapename();
            cout<<"r:"<<r<<endl;
            cout<<"area:"<<area()<<endl;
    }
};

 

class Square:public circle  /*衍生類別正方形*/
{
private:
 float a;
public:
 Square(){a=0;}
 Square(float b){a=b;}
    float area() const
 {
     return (a*a);
 }
 void shapename()const
 {
        cout<<"shapename:Square"<<endl;
 }
 void show()const                              /*輸出正方形資訊*/
 {
  shapename();
        cout<<"a:"<<a<<endl;
        cout<<"area:"<<area()<<endl;
 }
};

 

class Rectangle:public Square     /*衍生類別矩形*/
{
private:
 float a,b;
public:
 Rectangle(){a=0;b=0;}
 Rectangle(float c,float d){a=c;b=d;}
    float area()const
 {
       return (a*b);
 }

 void shapename() const
 {
 cout<<"shapename:Rectangle"<<endl;
 }
 void show()const                             /*輸出巨型資訊*/
 {
 shapename();
    cout<<"a:"<<a<<endl;
 cout<<"b:"<<b<<endl;
 cout<<"area:"<<area()<<endl;
 }

};

 

class Trapezoid:public shape        /*衍生類別梯形*/
{
private:
 float a,b,h;
public:
 Trapezoid(){a=0;b=0;h=0;}
 Trapezoid(float c,float d,float e){a=c;b=d;h=e;}
    float area()const
 {
 return ((a+b)*h)/2;
 }
 void shapename()const
 {
 cout<<"shapename:Trapezoid"<<endl;
 }
 void show()const                 /*輸出梯形資訊*/
 {
 shapename();
    cout<<"a:"<<a<<endl;
 cout<<"b:"<<b<<endl;
 cout<<"h:"<<h<<endl;
 cout<<"area:"<<area()<<endl;
 }

};

 

class Triangle:public shape        /*衍生類別三角形*/

{
private:
 float a,b,c;
public:
  Triangle(){a=0;b=0;c=0;}
  Triangle(float d,float e,float f){a=d;b=e;c=f;}
    float area()const                         /*求三角形面積*/
 {
  float S;
  float s=(a+b+c)/2;
     S=sqrt(s*(s-a)*(s-b)*(s-c));
 return S;
 }
 void shapename()const         
 {
 cout<<"shapename: Triangle"<<endl;
 }

 void show()const                       /*輸出三角形三邊長度及面積大小*/
 {
 shapename();
    cout<<"a:"<<a<<endl;
 cout<<"b:"<<b<<endl;
 cout<<"c:"<<c<<endl;
 cout<<"area:"<<area()<<endl;
 }

    int Istriangle();                                       /*判斷是否是三角形*/
};

int  Triangle::Istriangle()           
{
if((a+b)>c&&(b+c)>a&&(a+c)>b)                       /*使用兩邊之和大於第三邊判斷*/
 {
  cout<<"a="<<a<<" b2="<<b<<" c="<<c<<"."<<endl;           /*法輸出邊長*/
  return 1;
 }
 else
 {
  cout<<"輸入的邊長不符合三角形條件"<<endl;                /*邊長輸入錯誤*/
  return 0;
 }

}

 

 

int  main()
{int i;
shape *p[5];
for(i=0;i<5;i++)
p[i]=NULL;

float a,b,c;

do{
cout<<"/t/t*************************************"<<endl;
cout<<"/t/t             請選擇菜單              "<<endl;
cout<<"/t/t             1.求圓面積              "<<endl;
cout<<"/t/t             2.求正方形面積          "<<endl;
cout<<"/t/t             3.求長方形面積          "<<endl;
cout<<"/t/t             4.求梯形面積            "<<endl;
cout<<"/t/t             5.求三角形面積          "<<endl;
cout<<"/t/t             6.求所有形狀圖形面積的和"<<endl;
cout<<"/t/t             0.退出                  "<<endl;
cout<<"/t/t*************************************"<<endl;
cin>>i;
switch(i)
{
case 1:   {
           cout<<"請輸入圓半徑R,橫座標X,縱座標Y:";
           cin>>a>>b>>c;
           circle C(a,b,c);
     p[0]=&C;
     p[0]->show();
    }break;
case 2:   {
           cout<<"請輸入正方形的邊長A:";
           cin>>b;
           Square S(b);
     p[1]=&S;
     p[1]->show();
    }break;
case 3:{
           cout<<"請輸入長方形的長和寬A,B:";
           cin>>a>>b;
           Rectangle R(a,b);
     p[2]=&R;
     p[2]->show();
    }break;
case 4:{
           cout<<"請輸入梯形上底,下底,高A,B,H:";
           cin>>a>>b>>c;
           Trapezoid Tra(a,b,c);
     p[3]=&Tra;
     p[3]->show();
    }break;
case 5:{
           cout<<"請輸入三角形的三邊A,B,C:";
           cin>>a>>b>>c;
           Triangle Tri(a,b,c);
     p[4]=&Tri;
     if(Tri.Istriangle())           /*判斷是否符合三角形三邊的條件*/
     p[4]->show();
    }break; 
case 6:{float k=0;
    for(i=0;i<5;i++)
    {if(p[i]==NULL)
     k=k+0;
    else k=k+p[i]->area();}
    cout<<"總面積為:"<<k<<endl;
    }
   break;

}
}while(i!=0);

 

 

return 0;
}

              

聯繫我們

該頁面正文內容均來源於網絡整理,並不代表阿里雲官方的觀點,該頁面所提到的產品和服務也與阿里云無關,如果該頁面內容對您造成了困擾,歡迎寫郵件給我們,收到郵件我們將在5個工作日內處理。

如果您發現本社區中有涉嫌抄襲的內容,歡迎發送郵件至: info-contact@alibabacloud.com 進行舉報並提供相關證據,工作人員會在 5 個工作天內聯絡您,一經查實,本站將立刻刪除涉嫌侵權內容。

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.