C++ 多态:Shape类层次结构

创建 Shape类

1.创建基类Shape

class Shape
{
public:
	virtual double getArea(){}//计算面积 
	virtual double getVolume(){}//计算体积
	virtual void print(){}//打印 
	Shape(){}//构造 
	~Shape(){}//析构 
		
};

2.创建TwoDimensionalShape类,继承于Shape

class TwoDimensionalShape : public Shape//二维 
{
public:
	TwoDimensionalShape(){}
	virtual double getArea(){}
	virtual double getVolume(){}
	virtual void print(){}
};

3.创建ThreeDimensionalShape类,继承于Shape类

class ThreeDimensionalShape : public Shape//三维 
{
public:
	ThreeDimensionalShape(){}
	virtual double getArea(){}
	virtual double getVolume(){}
	virtual void print(){}
};
4.创建圆形类Circle,正方形类Square,三角形类Triangle,继承于二维类TwoDimensionalShape
class Circle : public TwoDimensionalShape//二维的圆形 
{
public:
	Circle(){}
	Circle(double radius)
	{
		radius_m=radius;
	}
	double getArea()
	{
		return E*radius_m*radius_m;
	}
	double getVolume(){}
	void print()
	{
		cout<<"二维的圆形的面积为:"<
5.创建球类Sphere,立方体类Cube,四面体类Tetrahedron,继承于三维类ThreeDimensionalShape
class Sphere : public ThreeDimensionalShape//三维的球 
{
public:
	Sphere(){}
	Sphere(double radius)
	{
		radius_m=radius;
	}
	double getArea()
	{
		return 4*E*radius_m*radius_m;
	}
	double getVolume()
	{
		return (4/3)*E*radius_m*radius_m*radius_m;
	}
	void print()
	{
		cout<<"三维的球的面积为:"<

6.测试程序

int main()
{
	//抽象类是不能实例化对象,所以想创建Shape类的vector对象,Shape类就不可以为抽象类 
	vector shapeclass;
	Shape *ptr_Shape=new Shape;
	Shape *ptr_Circle=new Circle(5);
	Shape *ptr_Square=new Square(4,5);
	Shape *ptr_Triangle=new Triangle(6,8);
	Shape *ptr_Sphere=new Sphere(3);
	Shape *ptr_Cube=new Cube(2,2,2);
	Shape *ptr_Tetrahedron=new Tetrahedron(4);
	
	shapeclass.push_back(ptr_Shape);
	shapeclass.push_back(ptr_Circle);
	shapeclass.push_back(ptr_Square);
	shapeclass.push_back(ptr_Triangle);
	shapeclass.push_back(ptr_Sphere);
	shapeclass.push_back(ptr_Cube);
	shapeclass.push_back(ptr_Tetrahedron);
	
	vector::iterator iter;
	for(iter = shapeclass.begin(); iter != shapeclass.end(); ++iter)
    {
        (*iter)->print();
    }
	delete ptr_Shape;
	delete ptr_Circle;
	delete ptr_Square;
	delete ptr_Triangle;
	delete ptr_Sphere;
	delete ptr_Cube;
	delete ptr_Tetrahedron;
	return 0;
}

完整代码如下:

//
//习题13.13 
//(Shape类层次结构)实现12.7中(基于图12.3的类层次结构)设计的Shape类层次结构 
//
#include
#include
#include
using namespace std;
#define E 3.14 
//Shape类 
class Shape
{
public:
	virtual double getArea(){}//计算面积 
	virtual double getVolume(){}//计算体积
	virtual void print(){}//打印 
	Shape(){}//构造 
	~Shape(){}//析构 
		
};

class TwoDimensionalShape : public Shape//二维 
{
public:
	TwoDimensionalShape(){}
	virtual double getArea(){}
	virtual double getVolume(){}
	virtual void print(){}
};

class ThreeDimensionalShape : public Shape//三维 
{
public:
	ThreeDimensionalShape(){}
	virtual double getArea(){}
	virtual double getVolume(){}
	virtual void print(){}
};

class Circle : public TwoDimensionalShape//二维的圆形 
{
public:
	Circle(){}
	Circle(double radius)
	{
		radius_m=radius;
	}
	double getArea()
	{
		return E*radius_m*radius_m;
	}
	double getVolume(){}
	void print()
	{
		cout<<"二维的圆形的面积为:"< shapeclass;
	Shape *ptr_Shape=new Shape;
	Shape *ptr_Circle=new Circle(5);
	Shape *ptr_Square=new Square(4,5);
	Shape *ptr_Triangle=new Triangle(6,8);
	Shape *ptr_Sphere=new Sphere(3);
	Shape *ptr_Cube=new Cube(2,2,2);
	Shape *ptr_Tetrahedron=new Tetrahedron(4);
	
	shapeclass.push_back(ptr_Shape);
	shapeclass.push_back(ptr_Circle);
	shapeclass.push_back(ptr_Square);
	shapeclass.push_back(ptr_Triangle);
	shapeclass.push_back(ptr_Sphere);
	shapeclass.push_back(ptr_Cube);
	shapeclass.push_back(ptr_Tetrahedron);
	
	vector::iterator iter;
	for(iter = shapeclass.begin(); iter != shapeclass.end(); ++iter)
    {
        (*iter)->print();
    }
	delete ptr_Shape;
	delete ptr_Circle;
	delete ptr_Square;
	delete ptr_Triangle;
	delete ptr_Sphere;
	delete ptr_Cube;
	delete ptr_Tetrahedron;
	return 0;
}

你可能感兴趣的:(C++,c++)