prog8.cpp

来自「C++语言程序设计题典」· C++ 代码 · 共 81 行

CPP
81
字号
#include <iostream.h>
const Max=20;
class base
{
	int x,y;
public:
	base(int i,int j) { x=i;y=j; }
	void move() { cout << "将画笔移动到坐标(" << x << "," << y << ")"; }
	virtual void draw()=0;
};
class Point:public base
{
public:
	Point(int i,int j):base(i,j) {}
	void draw()
	{
		base::move();
		cout << ",画一个点。" << endl;
	}
};
class Circle:public base
{
	int r;
public:
	Circle(int i,int j,int k):base(i,j)
	{
		r=k;
	}
	void draw()
	{
		base::move();
		cout << ",画一个半径为" << r << "的圆。" << endl;
	}
};
class Line:public base
{
	int x1,y1;
public:
	Line(int i,int j,int k,int l):base(i,j)
	{
		x1=k;y1=l;
	}
	void draw()
	{
		base::move();
		cout << ",画一条到坐标(" << x1 << "," << y1 << ")的直线。" << endl;
	}
};
class Graphics
{
	int top;
	base *elems[Max];
public:
	Graphics() { top=0; }
    void push(base *g)
	{
		elems[top]=g;
		top++;
	}
	void draw()
	{
		for (int i=0;i<top;i++)
		{
			cout << "第" << i+1 << "步:";
			elems[i]->draw();
		}
	}
};
void main()
{
	Graphics g;
	g.push(new Point(3,5));
	g.push(new Line(1,2,3,5));
	g.push(new Circle(3,4,5));
	g.push(new Line(8,6,21,15));
	g.push(new Circle(8,2,3));
	g.push(new Point(6,9));
	cout << "画图步骤:" << endl;
	g.draw();
}

⌨️ 快捷键说明

复制代码Ctrl + C
搜索代码Ctrl + F
全屏模式F11
增大字号Ctrl + =
减小字号Ctrl + -
显示快捷键?