#include "stdafx.h"
class CShap
{
public:
virtual ~CShap(){};
virtual void AddData(float a){}; //输入数据
virtual void AddData(float a, float b){}; //输入数据
virtual void AddData(float a, float b, float c, float d){}; //输入数据
virtual float Perimeter() = 0;//求周长
virtual float Area() = 0; //求面积
};
//三角形
class CT : public CShap
{
private:
float m_botton;
float m_height;
float m_sidLeft;
float m_sidRight;
public:
CT():
m_botton(0.0f),
m_height(0.0f),
m_sidLeft(0.0f),
m_sidRight(0.0f)
{
};
virtual ~CT(){};
//输入数据
virtual void AddData(float height, float botton, float sidLeft, float sidRight)
{
m_botton = botton;
m_height = height;
m_sidLeft = sidLeft;
m_sidRight = sidRight;
}
//求周长
virtual float Perimeter()
{
return m_botton + m_sidLeft + m_sidRight;
}
//求面积
virtual float Area()
{
return m_botton * m_height / 2.0f;
}
};
//园
class CC : public CShap
{
private:
float m_radius;
public:
CC():
m_radius(0.0f)
{
}
virtual ~CC(){};
//输入数据
virtual void AddData(float radius)
{
m_radius = radius;
}
//求周长
virtual float Perimeter()
{
return 2.0f * 3.14f * m_radius;
}
//求面积
virtual float Area()
{
return (2.0f * 3.14f) * (m_radius * m_radius);
}
};
//矩形
class CR : public CShap
{
private:
float m_width;
float m_height;
public:
CR():
m_width(0.0f),
m_height(0.0f)
{
}
virtual ~CR(){};
//输入数据
virtual void AddData(float width, float height)
{
m_width = width;
m_height = height;
}
//求周长
virtual float Perimeter()
{
return (m_width + m_height) * 2.0f;
}
//求面积
virtual float Area()
{
return m_width * m_height;
}
};
int _tmain(int argc, _TCHAR* argv[])
{
CShap* pCC = new CC();
CShap* pCT = new CT();
CShap* pCR = new CR();
pCC->AddData(12);
pCT->AddData(12,32,12,32);
pCR->AddData(123,32);
float ccArea = pCC->Area();
float ctArea = pCT->Area();
float crArea = pCR->Area();
float ccPerimeter = pCC->Perimeter();
float ctPerimeter = pCT->Perimeter();
float crPerimeter = pCR->Perimeter();
delete pCC;
delete pCT;
delete pCR;
return 0;
}