2. 定义一个抽象类Shape,包含纯虚函数Area()(用来计算面积)和SetShape()(用来重设形状大小)。然后派生出三角形类Triangle类、矩形Rect类、圆Circle类,分别求其面积

#include 
#include
using namespace std;
class  shape
{   public:
    virtual double area()=0;
    virtual void setshape()=0;
};
class triangle:public shape
{
    private:
    double length1,length2,length3;
    public:
    virtual void setshape()
    {
         cin>>length1>>length2>>length3;
    }

   virtual double area()
   {
       double  p;
       p=(length1+length2+length3)/2;
       double s=sqrt(p*(p-length1)*(p-length2)*(p-length3));

       return s;
   }

   };

   class rect:public shape
   {
   private:
    int a,b;
   public:
    virtual void setshape();
    virtual double area();
   };

  void rect::setshape()
{
   cin>>a>>b;

}

 double rect::area()
{
    int f;
    f=a*b;

    return f;
}
class circle:public shape
{
private:
    double r;
public:
    void setshape();
    double area();
};
void circle::setshape()
{
    cin>>r;
}
double circle::area()
{
    double q;
    double pi=3.14;

    q=pi*r*r;
    return q;
}
class totalarea:public triangle,public rect,public circle
{

public:
    double output(triangle ds,rect oo,circle ii)
    {
    double totalarea1 ;
    totalarea1=ds.area()+oo.area()+ii.area();
    return totalarea1;
    }
};




int main()
{
    triangle ds;
    ds.setshape();
    rect oo;
    oo.setshape();
    circle ii;
    ii.setshape();
    cout<


你可能感兴趣的:(2. 定义一个抽象类Shape,包含纯虚函数Area()(用来计算面积)和SetShape()(用来重设形状大小)。然后派生出三角形类Triangle类、矩形Rect类、圆Circle类,分别求其面积)