虚函数的应用(计算矩形和圆的面积及周长)

作者在 2014-05-02 23:25:09 发布以下内容

class Shape
{
public:
	Shape() {}
	~Shape() {}
	virtual float GetArea()=0;
	virtual float GetPerim()=0;
};

class Circle:public Shape
{
public:
	Circle(float radius):itsRadius(radius) {}
	float GetArea() 
	{
		return 3.14*itsRadius*itsRadius;
	}
	float GetPerim()
	{
		return 6.28*itsRadius;
	}
private:
	float itsRadius;
};

class Rectangle :public Shape
{
public:
	Rectangel(float len, float width):itsLength(len),itsWidth(width) {};
	~Rectangel (){};
	virtual float GetArea()
	{
		return itsLength* itsWidth;
	}
	float GetPerim()
	{
		return 2*itsLength+2*itsWidth;
	}
	virtual float GetLength()
	{
		return itsLength;
	}
	virtual float GetWidth()
	{
		return itsWidth;
	}
private:
	float itsWidth;
	float itsLength;
};
c++ | 阅读 1358 次
文章评论,共0条
游客请输入验证码
浏览37868次
最新评论