阅读下列说明和C++代码,填写程序中的空(1)~(6),将解答写入答题纸的对应栏内。
【说明】
以下C++代码实现一个简单绘图工具,绘制不同形状以及不同颜色的图形。部分类及其关系如图6-1所示。
【C++代码】
#include <iostream>
#include <string>
using namespace std;
class DrawCircle { //绘制圆形,抽象类
public:
(1) ;//定义参数为 int radius, int x, int y
virtual~DrawCircle() { }
};
class RedCircle:public DrawCircle { //绘制红色圆形
public:
void drawCircle(int radius, int x, int y) {
cout << "Drawing Circle[red,radius: " <<radius;
cout << ",x: " <<x<< ",y: "<<y<< "]" << end1;
}
};
class GreenCircle:public DrawCircle { //绘制绿色圆形
public:
void drawCircle(int radius, int x, int y) {
cout <<"Drawing Circle[green,radius: " << radius;
cout <<",x: " <<x<< ",y: " <<y<< "]" << end1;
}
};
class Shape { //形状,抽象类
protected:
(2) ;
public:
Shape(DrawCircle *drawCircle) {
this -> drawCircle = drawCircle;
}
virtual~shape() { }
public:
virtual void draw() = 0;
};
class Circle:public Shape { //圆形
private:
int x,y,radius;
public:
Circle(int x,int y,int radius,DrawCircle *drawCircle) (3) {
this->x = x;
this->y = y;
this->radius = radius;
}
public:
void draw() {
drawCircle -> (4) ;
}
};
int main()
{
Shape *redCircle=new Circle(100,100,10, (5) );//绘制红色圆形
Shape *greenCircle=new Circle(100,100,10, (6) );//绘制绿色圆形
redCircle ->draw();
greenCircle ->draw();
return 0;
}
上机测试了一下:得到的结论答案:某些网站给的错误的答案
(1)virtual void drawCircle(int radius, int x, int y)=0; //因为下面的class Circle的draw调用了,所以需要是纯虚函数。
(2)DrawCircle *drawCircle
(3):Shape(drawCircle)
(4)drawCircle(radius, x, y)
(5)new RedCircle()
(6)new GreenCircle()
一下可以测试一下跑在vs2005下代码:
#include <iostream>
#include <string>
using namespace std;
class DrawCircle { //绘制圆形,抽象类
public:
virtual void drawCircle(int radius, int x, int y)=0;//定义参数为 int radius, int x, int y,-----(1)
virtual~ DrawCircle() { }
};
class RedCircle : public DrawCircle { //绘制红色圆形
public:
void drawCircle(int radius, int x, int y) {
cout << "Drawing Circle[red,radius: " << radius;
cout << ",x: " << x << ",y: " << y << "]" << endl;
}
};
class GreenCircle : public DrawCircle { //绘制绿色圆形
public:
void drawCircle(int radius, int x, int y) {
cout << "Drawing Circle[green,radius: " << radius;
cout << ",x: " << x << ",y: " << y << "]" << endl;
}
};
class Shape { //形状,抽象类
protected:
DrawCircle* drawCircle;///-------------------(2)
public:
Shape(DrawCircle *drawCircle) {
this->drawCircle = drawCircle;
}
virtual~ Shape() { }
public:
virtual void draw()=0;
};
class Circle : public Shape { //圆形
private:
int x, y, radius;
public:
Circle(int x, int y, int radius, DrawCircle *drawCircle):Shape(drawCircle) {//----------------------(3)
this->x = x;
this->y = y;
this->radius = radius;
}
public:
void draw() {
drawCircle->drawCircle(radius, x, y);//--------------(4)
}
};
int main()
{
Shape *redCircle = new Circle(200, 200, 20, new RedCircle());//绘制红色圆形//---------------(5)
Shape *greenCircle = new Circle(10, 100, 10, new GreenCircle());//绘制绿色圆形//-------------------(6)
redCircle->draw();
greenCircle->draw();
while(1){};
return 0;
}
运行结果:
Drawing Circle[red,radius: 20,x: 200,y: 200]
Drawing Circle[green,radius: 10,x: 10,y: 100]