#include<iostream>
#include<cmath>
using namespace std;
double const pai=3.1415926;
const int N=5;
class Shape
{
public:
Shape(){};
virtual double PrintArea() const =0;
protected:
};
class Triangle:public Shape
{
public:
Triangle(double a,double b,double c):length1(a),length2(b),length3(c){}
virtual double PrintArea() const;
protected:
double length1,length2,length3;
};
double Triangle::PrintArea() const
{
if(length1+length2<=length3||abs(length1-length2)>=length3||length1<=0||length2<=0||length3<=0)
{
cout<<"所输入的数不能构成三角形"<<endl;
system("pause");
return -1;
}
else
{
double p=(length1+length2+length3)/2;
return sqrt(p*(p-length1)*(p-length2)*(p-length3));
}
}
class Circle :public Shape
{
public:
Circle(double c):radius(c){}
virtual double PrintArea() const;
protected:
double radius;
};
double Circle::PrintArea() const
{
return pai*radius*radius;
}
class Rectangle:public Shape
{
public:
Rectangle(double a, double b):length(a),width(b){}
virtual double PrintArea() const;
protected:
double length,width;
};
double Rectangle::PrintArea() const
{
return length*width;
}
class Square:public Shape
{
public:
Square(double a):length(a){}
virtual double PrintArea() const;
protected:
double length;
};
double Square::PrintArea()const
{
return length*length;
}
class Trapezoid:public Shape
{
public:
Trapezoid(double a,double b,double c):lengthup(a),lengthdown(b),height(c){}
virtual double PrintArea() const;
protected:
double lengthup,lengthdown,height;
};
double Trapezoid::PrintArea() const
{
return (lengthup+lengthdown)/2*height;
}
int main()
{
Circle a(10);
Square b(8);
Rectangle c(5,2.5);
Trapezoid d(2,4,6);
Triangle e(3,4,5);
Shape *p[N]={&a,&b,&c,&d,&e};
cout<<"以下分别是园,正方形,矩形,梯形,三角形"<<endl;
for(int i=0;i<N;i++)
{
cout<<p[i]->PrintArea()<<endl;
}
system("pause");
return 0;
}