定义一个抽象类Shape,包含纯虚函数Area()(用来计算面积)和SetShape()(用来重设形状大小)

 定义一个抽象类Shape,包含纯虚函数Area()(用来计算面积)和SetShape()(用来重设形状大小)。然后派生出三角形类Triangle类、矩形Rect类、圆Circle类,分别求其面积。最后定义一个Total_Area类,计算这几个形状的面积之和,各形状的数据通过Total_Area类的构造函数或成员函数来设置。编写一个完整的程序。


#include


using namespace std;


class shape
{
public:
    virtual double Area()=0;
    virtual void Setshape()=0;
};




class Triangle:public shape
{
private :
    float floor,high;
public:
    Triangle();
    ~Triangle();
    virtual double Area();
    virtual void Setshape();
};




Triangle::Triangle()
{
}
Triangle::~Triangle()
{
    cout<<"Triangle_clear"<}
double Triangle::Area()
{
    return floor*high/2;
}
void Triangle::Setshape()
{
    cout<<"输入三角形长与宽"<    cin>>floor;
    cin>>high;
}




class Rect:public shape
{
private:
    double len,width;
public:
    Rect();
    Rect(int len,int width);
    ~Rect();
    virtual double Area();
    virtual void Setshape();
};




Rect::Rect()
{
}
Rect::Rect(int l,int w)
{
    this->len=l;
    this->width=w;
}
Rect::~Rect()
{
    cout<<"RECT_clear"<}
double Rect::Area()
{
    return len*width;
}
void Rect::Setshape()
{
    cout<<"输入矩阵的才长和宽"<    cin>>len;
    cin>>width;
}




class Circle:public shape
{
private :
    double r;
public :
    Circle();
    Circle(int r);
    ~Circle();
    virtual double Area();
    virtual void Setshape();
};


Circle::Circle()
{
}
Circle::Circle(int R)
{
    this->r=R;
}
Circle::~Circle()
{
    cout<<"Circle_clear"<}
double Circle::Area()
{
    return 3.1415*r*r;
}
void Circle::Setshape()
{
    cout<<"输入圆的半径"<    cin>>r;
}


class Total_Area:public Triangle,public Rect,public Circle
{
public:
    Total_Area();
    ~Total_Area();
    double allArea(shape *a,shape *b,shape *c);
};
Total_Area::Total_Area()
{
}
Total_Area::~Total_Area()
{
    cout<<"all_Area_Clear"<

}
double Total_Area::allArea(shape *a,shape *b,shape *c)
{
    double area;
    area=a->Area()+b->Area()+c->Area();
    return area;
}


int main()
{
    Triangle triangle;
    triangle.Setshape();
    cout<<"triangle area: "<

    Rect rect;
    rect.Setshape();
    cout<<"rect area: "<

    Circle circle;
    circle.Setshape();
    cout<<"circle area: "<

    Total_Area allArea;
    cout<<"总面积是: "<}

你可能感兴趣的:(c++)