#include <iostream>
#define _USE_MATH_DEFINES
#include <cmath>
using namespace std;
class CShape
{
public:
virtual double Area() = 0; // 求面积
virtual double Perimeter() = 0; // 求周长
};
// 圆
class CCircle : public CShape
{
protected:
double m_nRadius; // 圆的半径
public:
CCircle(double nRadius)
: m_nRadius(nRadius)
{
}
virtual ~CCircle()
{
}
virtual double Area()
{
return M_PI * m_nRadius * m_nRadius;
}
virtual double Perimeter()
{
return 2 * M_PI * m_nRadius;
}
};
// 圆内接正方形
class CSquareInCircle : public CShape
{
protected:
double m_nRadius; // 圆的半径
public:
CSquareInCircle(double nRadius)
: m_nRadius(nRadius)
{
}
virtual ~CSquareInCircle()
{
}
virtual double Area()
{
return (2 * m_nRadius * m_nRadius);
}
virtual double Perimeter()
{
return (4 * sqrt(2.0) * m_nRadius);
}
};
// 圆外接正方形
class CSquareOutCircle : public CShape
{
protected:
double m_nRadius; // 圆的半径
public:
CSquareOutCircle(double nRadius)
: m_nRadius(nRadius)
{
}
virtual ~CSquareOutCircle()
{
}
virtual double Area()
{
return (2 * m_nRadius * 2 * m_nRadius);
}
virtual double Perimeter()
{
return (4 * 2 * m_nRadius);
}
};
///
int main(int argc, char* argv[])
{
CShape* pShape = NULL;
CCircle circle(3);
CSquareInCircle square1(3);
CSquareOutCircle square2(3);
pShape = &circle;
cout<<"圆的面积: "<<pShape->Area()<<endl;
cout<<"圆的周长: "<<pShape->Perimeter()<<endl;
pShape = &square1;
cout<<"圆内接正方形的面积: "<<pShape->Area()<<endl;
cout<<"圆内接正方形的周长: "<<pShape->Perimeter()<<endl;
pShape = &square2;
cout<<"圆外接正方形的面积: "<<pShape->Area()<<endl;
cout<<"圆外接正方形的周长: "<<pShape->Perimeter()<<endl;
return 0;
}