#include <iostream>
#include <cmath>
using namespace std;
class Graphical
{
protected:
float perm;
float area;
public:
void virtual perimeter()=0;
void virtual extent()=0;
Graphical(){}//无参构造函数
virtual ~Graphical(){}
};
//矩形
class Rectangle:public Graphical
{
private:
float length;
float wide;
public:
Rectangle(){}
Rectangle(float i_length,float i_wide):length(i_length),wide(i_wide){}
void perimeter() override
{
perm = (length+wide)*2;
cout<<"矩形的周长是:"<<perm<<endl;
return;
}
void extent() override
{
area = length*wide;
cout<<"矩形的面积是:"<<area<<endl;
return;
}
~Rectangle(){}
};
//圆形
class Circle:public Graphical
{
private:
float radius;//半径
float pai = 3.14;
public:
Circle(){}
Circle(float i_radius):radius(i_radius){}
void perimeter() override
{
perm = radius*2*pai;
cout<<"圆形的周长是:"<<perm<<endl;
return;
}
void extent() override
{
area = radius*pai;
cout<<"圆形的面积是:"<<area<<endl;
return;
}
~Circle(){}
};
//三角形
class Sanjiao:public Graphical
{
private:
float a, b, c;
public:
Sanjiao(){}
Sanjiao(float i_a,float i_b,float i_c):a(i_a),b(i_b),c(i_c){}
void perimeter() override
{
perm = a+b+c;
cout<<"三角形的周长是:"<<perm<<endl;
return;
}
void extent() override
{
float p =( a+b+c)/2;
area = sqrt(p*(p-a)*(p-b)*(p-c));
cout<<"三角形的面积是:"<<area<<endl;
return;
}
~Sanjiao(){}
};
void output(Graphical &tuxing)
{
tuxing.perimeter();
tuxing.extent();
}
/*
void Rectangle_fun(float len,float hight)
{
Rectangle s1(len,hight);
output(s1);
return;
}
void Circle_fun(float r)
{
Circle s2(r);
output(s2);
return;
}
void Sanjiao_fun(float a,float b,float c)
{
Sanjiao s3(a,b,c);
output(s3);
return;
}
*/
int main(int argc, const char *argv[])
{
int change;
while(1)
{
cout<<"请输入你要选择的图形:\n1.矩形\n2.圆形\n3.三角形"<<endl;
cin>>change;
switch(change)
{
case 1:
{
cout<<"请输入矩形的长宽:"<<endl;
float len,hight;
cin>>len>>hight;
Rectangle s1(len,hight);
output(s1);
break;
}
case 2:
{
cout<<"请输入圆形的半径:"<<endl;
float r;
cin>>r;
Circle s2(r);
output(s2);
break;
}
case 3:
{
cout<<"请分别输入三角形的三边:"<<endl;
float a,b,c;
cin>>a>>b>>c;
if( (a+b)>c && (a+c)>b && (b+c)>a )
{
Sanjiao s3(a,b,c);
output(s3);
break;
}
else
{
cout << "这三边不构成三角形\n";
continue;
}
}
default:
{
cout<<"选择有误,请重新选择"<<endl;
continue;
}
}
}
return 0;
}