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<<"二维的圆形的面积为:"<<getArea()<<endl;	
	}	
private:
	double radius_m;//圆的半径 
};
class Square : public TwoDimensionalShape//二维的正方形 
{
public:
	Square(){}
	Square(double length,double width)
	{
		length_m=length;
		width_m=width;
	}
	double getArea()
	{
		return length_m*width_m;
	}
	double getVolume(){}
	void print()
	{
		cout<<"二维的正方形的面积为:"<<getArea()<<endl;	
	}
private:
	double length_m;//长 
	double width_m;//宽 
};
class Triangle : public TwoDimensionalShape//二维的三角形 
{
public:
	Triangle(){}
	Triangle(double length,double height)
	{
		length_m=length;
		height_m=height;
	}
	double getArea()
	{
		return 0.5*length_m*height_m;
	}
	double getVolume(){}
	void print()
	{
		cout<<"二维的三角形的面积为:"<<getArea()<<endl;	
	}
private:
	double length_m;//底 
	double height_m;//高 
};
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<<"三维的球的面积为:"<<getArea()<<endl;
		cout<<"三维的球的体积为:"<<getVolume()<<endl;	
	}
private:
	double radius_m;//球的半径 
};
class Cube : public ThreeDimensionalShape//三维的立方体
{
public:
	Cube(){}
	Cube(double length,double width,double height)
	{
		length_m=length;
		width_m=width;
		height_m=height;
	}
	double getArea()
	{
		return 6*length_m*width_m;
	}
	double getVolume()
	{
		return length_m*width_m*height_m;
	}
	void print()
	{
		cout<<"三维的立方体的面积为:"<<getArea()<<endl;
		cout<<"三维的立方体的体积为:"<<getVolume()<<endl;	
	}
private:
	double length_m;//长 
	double width_m;//宽 
	double height_m;//高 
};
class Tetrahedron : public ThreeDimensionalShape//三维的四面体(三棱锥)在这里我定义它为正四面体 
{
public:
	Tetrahedron(){}
	Tetrahedron(double edge)
	{
		edge_m=edge;
	}
	double getArea()
	{
		return sqrt(3)*edge_m*edge_m;
	}
	double getVolume()
	{
		return (sqrt(2)/12)*pow(edge_m,3);
	} 
	void print()
	{
		cout<<"三维的四面体的面积为:"<<getArea()<<endl;
		cout<<"三维的四面体的体积为:"<<getVolume()<<endl;	
	}
private:
	double edge_m; //棱长 
};

6.测试程序

int main()
{
	//抽象类是不能实例化对象,所以想创建Shape类的vector对象,Shape类就不可以为抽象类 
	vector<Shape*> 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<Shape*>::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;
}

完整代码如下:

/
//Shape类层次结构
/
#include<iostream>
#include<vector>
#include<math.h>
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<<"二维的圆形的面积为:"<<getArea()<<endl;	
	}	
private:
	double radius_m;//圆的半径 
};
class Square : public TwoDimensionalShape//二维的正方形 
{
public:
	Square(){}
	Square(double length,double width)
	{
		length_m=length;
		width_m=width;
	}
	double getArea()
	{
		return length_m*width_m;
	}
	double getVolume(){}
	void print()
	{
		cout<<"二维的正方形的面积为:"<<getArea()<<endl;	
	}
private:
	double length_m;//长 
	double width_m;//宽 
};
class Triangle : public TwoDimensionalShape//二维的三角形 
{
public:
	Triangle(){}
	Triangle(double length,double height)
	{
		length_m=length;
		height_m=height;
	}
	double getArea()
	{
		return 0.5*length_m*height_m;
	}
	double getVolume(){}
	void print()
	{
		cout<<"二维的三角形的面积为:"<<getArea()<<endl;	
	}
private:
	double length_m;//底 
	double height_m;//高 
};

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<<"三维的球的面积为:"<<getArea()<<endl;
		cout<<"三维的球的体积为:"<<getVolume()<<endl;	
	}
private:
	double radius_m;//球的半径 
};
class Cube : public ThreeDimensionalShape//三维的立方体
{
public:
	Cube(){}
	Cube(double length,double width,double height)
	{
		length_m=length;
		width_m=width;
		height_m=height;
	}
	double getArea()
	{
		return 6*length_m*width_m;
	}
	double getVolume()
	{
		return length_m*width_m*height_m;
	}
	void print()
	{
		cout<<"三维的立方体的面积为:"<<getArea()<<endl;
		cout<<"三维的立方体的体积为:"<<getVolume()<<endl;	
	}
private:
	double length_m;//长 
	double width_m;//宽 
	double height_m;//高 
};
class Tetrahedron : public ThreeDimensionalShape//三维的四面体(三棱锥)在这里我定义它为正四面体 
{
public:
	Tetrahedron(){}
	Tetrahedron(double edge)
	{
		edge_m=edge;
	}
	double getArea()
	{
		return sqrt(3)*edge_m*edge_m;
	}
	double getVolume()
	{
		return (sqrt(2)/12)*pow(edge_m,3);
	} 
	void print()
	{
		cout<<"三维的四面体的面积为:"<<getArea()<<endl;
		cout<<"三维的四面体的体积为:"<<getVolume()<<endl;	
	}
private:
	double edge_m; //棱长 
};
int main()
{
	//抽象类是不能实例化对象,所以想创建Shape类的vector对象,Shape类就不可以为抽象类 
	vector<Shape*> 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<Shape*>::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;
}

猜你喜欢

转载自blog.csdn.net/weixin_74287172/article/details/133175074