欲开发一个绘图软件,要求使用不同的绘图程序绘制不同的图形。以绘制直线和圆形为例,

考试题库2022-08-02  67

问题 欲开发一个绘图软件,要求使用不同的绘图程序绘制不同的图形。以绘制直线和圆形为例,对应的绘图程序如表5-1所示。表5-1不同的绘图程序该绘图软件的扩展性要求,将不断扩充新的图形和新的绘图程序。为了避免出现类爆炸的情况,现采用桥接(Bridge)模式来实现上述要求,得到如图5-1所示的类图。图5-1类图【C++代码】class?DP1{public:static void draw_a_line(double?x1,double?y1,double?x2,double?y2){/*代码省略*/}static void draw_a_circle(double?x,double?y,double?r){/*代码省略*/}};class?DP2{public:static void drawline(double x1,double x2,double y1,double y2){/*代码省略*/}static void drawcircle(double x,double y,double r){/*代码省略*/}};class?Drawing{public:(1);(2);};class V1Drawing:public Drawing{public:void drawLine(double?x1,double?y1,double?x2,double?y2){/*代码省略*/}void drawCircle(double?x,double?y,double?r){(3);}};class V2Drawing:public?Drawing{public:void drawLine(double?x1,double?y1,double?x2,double?y2){/*代码省略*/}void drawCircle(double?x,double y,double?r){(4);}};class Shape{public:(5);Shape(Drawing*dp){_dp=dp;}void drawLine(double?x1,double?y1,double?x2,double y2){_dp->drawLine(x1,y1,x2,y2);}void drawCircle(double?x,double?y,double r){_dp->drawCircle(x,y,r);}private:Drawing*_dp;};class?Rectangle:public?Shape{public:void draw(  ){/*代码省略*/}//其余代码省略};class Circle:public Shape{private:double_x,_y,_r;public:Circle(Drawing*dp,double x,double?y,double r):(6){_x=x;_y=y;_r=r;}void draw(  ){drawCircle(_x,_y,_r);}};

选项

答案

解析 (1)virtual void drawLine(double x1,double y1,double x2,double y2)=0(2)virtual void drawCircle(double x,double y,double r)=0(3)DP1::draw_a_circle(x,y,r)(4)DP2::drawcircle(x,y,r)(5)virtual void?draw()=0(6)Shape(dp)根据类图,可知:Drawing类有两个函数drawLine()、drawCircle(),在从继承于它的两个子类V1Drawing、V2Drawing中drawLine()、drawCircle()得到返回值类型为void及函数参数。则(1)为virtual void drawLine(double x1,double y1,double x2,double y2)=0;(2)为virtual void drawCircle(double x,double y,double r)=0。根据V1Drawing与DP1之间的关联关系,V2Drawing与DP2之间的关联关系,可知(3)是去调用DP1中的静态函数draw_a_circle(double x,double y,double r),则(3)为DP1::draw_a_circle(x,y,r);(4)问同理,答案为DP2::drawcircle(x,y,r)。(5)问从类图中可知,Shape有三个方法draw()、drawLine()、drawCircle();分析题目中给出的Shape的定义,少了一个draw()函数,可知(5)为draw(),在根据继承于Shape的Circle和Rectangle分析,函数返回值为void,参数为空,则(5)为virtual void draw()=0。(6)空考查继承结构中子类构造函数的定义。构造子类对象时,需要调用基类的构造函数,这可以通过初始化列表显式指明需要调用的基类的构造函数。在本题中,上文Shape类只定义了一个构造函数,所以这里填写的应该为“Shape(dp)”。
转载请注明原文地址:https://tihaiku.com/congyezige/2409644.html

最新回复(0)