图形绘制器
请修改编辑器中的代码,完成以下功能:实现一个图形绘制程序,其中包含两个类:IsocelesTriangles(等腰三角形)和Parallelogram(平行四边形)。每个类都继承自基类Graphics,并实现一个Draw()函数用于绘制相应的图形。Graphics类中定义了一个纯虚函数Draw(),要求子类实现。IsocelesTriangles类的Draw()函数应绘制一个由星号组成的等腰三角形,Parallelogram类的Draw()函数应绘制一个由星号组成的平行四边形。程序无需读取输入,直接在main函数中创建两个对象,分别绘制一个高为6行的等腰三角形和一个每行6个星号的平行四边形。 不需要输入
#include <iostream>
#include <iomanip>
using namespace std;
class Graphics //图形类
{
public:
Graphics(int e):edges(e) { }
virtual ~Graphics() { }
//**********found**********
____________________________
protected:
int edges;
};
class IsocelesTriangles : public Graphics //等腰三角形类
{
public:
IsocelesTriangles(int x):Graphics(x) { }
void Draw();
};
void IsocelesTriangles::Draw()
{
int i,j;
if(edges<=0)
cout<<"errors"<<endl;
if(edges>0)
{
for(i=0;i<edges;i++)
{
for(j=0;j<edges-i;j++)
cout<<setw(2)<<' ';
//**********found**********
for(j=0;____________ ;j++) //输出每行的*号
cout<<setw(2)<<'*';
cout<<endl;
}
}
cout<<endl;
}
//**********found**********
__________________________________ //平行四边形类
{
public:
Parallelogram(int x):Graphics(x) { }
void Draw();
};
void Parallelogram::Draw()
{
int i,j;
if(edges<=0)
cout<<"errors"<<endl;
if(edges>0)
{
for(i=0;i<edges;i++)
{
//**********found**********
for(j=0;_____________; j++) //输出前导空格
cout<<setw(2)<<' ';
for(j=0;j<edges;j++)
cout<<setw(2)<<'*';
cout<<endl;
}
}
cout<<endl;
}
int main()
{
Graphics *objs[2];
objs[0]=new IsocelesTriangles(6);
objs[1]=new Parallelogram(6);
for(int i=0;i<2;i++)
objs[i]->Draw();
delete objs[0];
delete objs[1];
return 0;
}
控制台
调试终端
