C++ 多态:(使用Shape类层次结构的多态性的屏幕管理器)开发一个基本图形软件包

基于上篇C++ 多态:Shape类层次结构icon-default.png?t=N7T8https://mp.csdn.net/mp_blog/creation/editor/133175074

【问题描述】


使用C++ 多态:Shape类层次结构中实现的Shape类层次。仅限于二维形状,如正方形,长方形、三角形和圆。和用户进行交互,让用户指定形状、位置和大小;以及绘制每个形状时所用的填充字符。用户可以指定多于一个的同样形状。当你创建每个形状时,都将指向新Shape 对象的一个Shape*指针添加到一个数组中。每个 Shape 类应用它自己的 draw 成员函数。编写一个遍历上述数组的多态的屏幕管理器,向数组中的每个对象发送draw消息,以形成屏幕图像。每次当用户指定新添的一个形状时,要重新绘制整个屏幕图像。


【具体方法】

1.创建Shape类

//Shape类 
class Shape
{
public:
	virtual double getArea(){}//计算面积 
	virtual double getVolume(){}//计算体积
	virtual void print(){}//打印 
	virtual void draw(int x,int y,char c){}//绘制形状 
	Shape(){}//构造 
	~Shape(){}//析构 	
};
2.创建二维类TwoDimensionalShape,继承于Shape类
class TwoDimensionalShape : public Shape//二维 
{
public:
	TwoDimensionalShape(){}
	virtual double getArea(){}
	virtual void print(){} 
	virtual void draw(int x,int y,char c){}
};
3.正方形类Square,继承于二维类TwoDimensionalShape
class Square : public TwoDimensionalShape//二维的正方形 
{
public:
	Square(){}
	Square(double side)
	{
		side_m=side; 
	}
	double getArea()
	{
		return side_m*side_m;
	}
	void print()
	{
		cout<<"二维的正方形的面积为:"<=x-1 && j=y-1 && i=x-2 && j=y-2 && i
4.长方形类Rectangle,继承于二维类TwoDimensionalShape
class Rectangle_ : public TwoDimensionalShape//二维的长方形 
{
public:
	Rectangle_(){}
	Rectangle_(double length,double width)
	{
		length_m=length;
		width_m=width;
	}
	double getArea()
	{
		return length_m*width_m;
	}
	void draw(int x,int y,char c)
	{
		int a[50][50];
		for(int i=0;i<50;i++)
		{
			for(int j=0;j<50;j++)
			{
				a[i][j]=0;
			}
		}
		for(int i=0;i<50;i++)
		{
			for(int j=0;j<50;j++)
			{
				if(j>=x-1 && j=y-1 && i=x-2 && j=y-2 && i
5.三角形类Triangle,继承于二维类TwoDimensionalShape

class Triangle : public TwoDimensionalShape//二维的三角形 (这里定义为等腰直角三角形)
{
public:
	Triangle(){}
	Triangle(double side)
	{
		side_m=side; 
	}
	double getArea()
	{
		return 0.5*side_m*side_m;
	}
	void draw(int x,int y,char c)
	{
		int a[50][50];
		for(int i=0;i<50;i++)
		{
			for(int j=0;j<50;j++)
			{
				a[i][j]=0;
			}
		}
		for(int i=0,k=-y+2;i<50;i++,k++)
		{
			for(int j=0;j<50;j++)
			{
				if(j>=x-1 && j=y-1 && i=x-2 && j=y-3 && i
6.圆形类Circle,继承于二维类TwoDimensionalShape (在这里我想说一下,关于画圆以及如何填充圆,推荐参考文章 Bresenham 画圆算法原理)
class Circle : public TwoDimensionalShape//二维的圆形 
{
public:
	Circle(){}
	Circle(double radius)
	{
		radius_m=radius;
	}
	double getArea()
	{
		return E*radius_m*radius_m;
	}
	void draw(int x,int y,char c)
	{
 		//1==0)
			{
				p+=4*(x-y)+10;
				y--;
			}
			else
			{
				p+=4*x+6;
			}
			
		}
		for(int i=0;i<50;i++)
		{
			cout<

7.绘制图形

char cinColor()
{
	char c;
	int color;
	cout<<"填充字符有:1.'*' 2.'#' 3.'@' 4.'&'"<>color; 
	if(color==1)
		c='*';
	else if(color==2)
		c='#';
	else if(color==3)
		c='@';
	else
		c='&';
	return c;
}

8.测试程序

int main()
{
	char cinColor();
	vector shapeclass;
	vector::iterator iter;
    int a;
    int x,y;
    a=-1;
	while(a!=0)
	{
	cout<<"******二维形状图形********"<>a; 
		switch(a)
		{
			case 1:
				{
					double side;
					cout<<"Please enter the side of square:"<>side;
					Shape *ptr_Square=new Square(side);
					shapeclass.push_back(ptr_Square);
					iter = shapeclass.begin();
					(*iter)->print();
					char ch=cinColor();
					cout<<"请输入图形的起始坐标:"<>x>>y; 
					(*iter)->draw(x,y,ch);
					delete ptr_Square;	
				}
				break;
			case 2:
				{
					double length,width;
					cout<<"Please enter the length and width of the rectangle:"<>length>>width;	
					Shape *ptr_Rectangle_=new Rectangle_(length,width);
					shapeclass.push_back(ptr_Rectangle_); 
					iter = shapeclass.begin();
					(*iter)->print();
					char ch=cinColor();
					cout<<"请输入图形的起始坐标:"<>x>>y; 
					(*iter)->draw(x,y,ch);
					delete ptr_Rectangle_;
				}
				break;
			case 3: 
				{
					double side;
					cout<<"Please enter the side of the triangle: "<>side;
					Shape *ptr_Triangle=new Triangle(side);
					shapeclass.push_back(ptr_Triangle);
					iter = shapeclass.begin();
					(*iter)->print();
					char ch=cinColor();
					cout<<"请输入图形的起始坐标:"<>x>>y; 
					(*iter)->draw(x,y,ch);
					delete ptr_Triangle;	
				}
				break;
			case 4:
				{
					double radius;
					cout<<"Please enter the radius of Circle(0>radius;
					Shape *ptr_Circle=new Circle(radius);
					shapeclass.push_back(ptr_Circle);
					iter = shapeclass.begin();
					(*iter)->print();
					char ch=cinColor();
					x=y=radius; 
					(*iter)->draw(x,y,ch);	
					delete ptr_Circle;	
				}
				break;
			case 0:
				{
					a=0;
				}
				break;	
		}
	    system("pause");
	    system("cls");
	    cout<

完整的代码如下:

//
//习题13.14
//(使用Shape类层次结构的多态性的屏幕管理器)开发一个基本图形软件包
/
#include
#include
#include
#include
using namespace std;
#define E 3.14 
//Shape类 
class Shape
{
public:
	virtual double getArea(){}//计算面积 
	virtual double getVolume(){}//计算体积
	virtual void print(){}//打印 
	virtual void draw(int x,int y,char c){}//绘制形状 
	Shape(){}//构造 
	~Shape(){}//析构 	
};

class TwoDimensionalShape : public Shape//二维 
{
public:
	TwoDimensionalShape(){}
	virtual double getArea(){}
	virtual void print(){} 
	virtual void draw(int x,int y,char c){}
};
class Square : public TwoDimensionalShape//二维的正方形 
{
public:
	Square(){}
	Square(double side)
	{
		side_m=side; 
	}
	double getArea()
	{
		return side_m*side_m;
	}
	void print()
	{
		cout<<"二维的正方形的面积为:"<=x-1 && j=y-1 && i=x-2 && j=y-2 && i=x-1 && j=y-1 && i=x-2 && j=y-2 && i=x-1 && j=y-1 && i=x-2 && j=y-3 && i=0)
			{
				p+=4*(x-y)+10;
				y--;
			}
			else
			{
				p+=4*x+6;
			}
			
		}
		for(int i=0;i<50;i++)
		{
			cout<>color; 
	if(color==1)
		c='*';
	else if(color==2)
		c='#';
	else if(color==3)
		c='@';
	else
		c='&';
	return c;
}

int main()
{
	char cinColor();
	vector shapeclass;
	vector::iterator iter;
    int a;
    int x,y;
    a=-1;
	while(a!=0)
	{
	cout<<"******二维形状图形********"<>a; 
		switch(a)
		{
			case 1:
				{
					double side;
					cout<<"Please enter the side of square:"<>side;
					Shape *ptr_Square=new Square(side);
					shapeclass.push_back(ptr_Square);
					iter = shapeclass.begin();
					(*iter)->print();
					char ch=cinColor();
					cout<<"请输入图形的起始坐标:"<>x>>y; 
					(*iter)->draw(x,y,ch);
					delete ptr_Square;	
				}
				break;
			case 2:
				{
					double length,width;
					cout<<"Please enter the length and width of the rectangle:"<>length>>width;	
					Shape *ptr_Rectangle_=new Rectangle_(length,width);
					shapeclass.push_back(ptr_Rectangle_); 
					iter = shapeclass.begin();
					(*iter)->print();
					char ch=cinColor();
					cout<<"请输入图形的起始坐标:"<>x>>y; 
					(*iter)->draw(x,y,ch);
					delete ptr_Rectangle_;
				}
				break;
			case 3: 
				{
					double side;
					cout<<"Please enter the side of the triangle: "<>side;
					Shape *ptr_Triangle=new Triangle(side);
					shapeclass.push_back(ptr_Triangle);
					iter = shapeclass.begin();
					(*iter)->print();
					char ch=cinColor();
					cout<<"请输入图形的起始坐标:"<>x>>y; 
					(*iter)->draw(x,y,ch);
					delete ptr_Triangle;	
				}
				break;
			case 4:
				{
					double radius;
					cout<<"Please enter the radius of Circle(0>radius;
					Shape *ptr_Circle=new Circle(radius);
					shapeclass.push_back(ptr_Circle);
					iter = shapeclass.begin();
					(*iter)->print();
					char ch=cinColor();
					x=y=radius; 
					(*iter)->draw(x,y,ch);	
					delete ptr_Circle;	
				}
				break;
			case 0:
				{
					a=0;
				}
				break;	
		}
	    system("pause");
	    system("cls");
	    cout<

你可能感兴趣的:(C++,c++,开发语言)