(四)Shape

#include<iostream>
#include<string>

using namespace std;

class DrawCircle
{
    public:
        // int radius,x,y;  /*定义参数为int radius, int x, int y*/

        virtual void drawCircle(int radius, int x, int y) = 0;  //(1)
        virtual ~DrawCircle()
        {

        }
};

class RedCircle : public DrawCircle
{
    /*绘制红色圆形*/
    public:
        void drawCircle(int radius, int x, int y)
        {
            cout << "DrawingCircle: red, radius: " <<radius <<" x: "<< x <<" y: "<< y <<endl;
        }
};

class GreenCircle : public DrawCircle
{
    /*绘制绿色圆形*/
    public:
        void drawCircle(int radius, int x, int y)
        {
            cout << "DrawingCircle: green, radius: " <<radius <<" 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)
        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(100,100,20,(new RedCircle()));  //(5)
    Shape *greenCircle = new Circle(100,100,20,(new GreenCircle()));  //(6)
    redCircle->draw();
    greenCircle->draw();
    return 0;
}

答案:
(1) virtual void drawCircle(int radius, int x, int y) = 0;

(2) DrawCircle* drawCircle;

(3) :Shape(drawCircle)

(4) drawCircle(radius, x, y);

(5) new RedCircle()

(6) new GreenCircle()

最后编辑于
©著作权归作者所有,转载或内容合作请联系作者
【社区内容提示】社区部分内容疑似由AI辅助生成,浏览时请结合常识与多方信息审慎甄别。
平台声明:文章内容(如有图片或视频亦包括在内)由作者上传并发布,文章内容仅代表作者本人观点,简书系信息发布平台,仅提供信息存储服务。

相关阅读更多精彩内容

  • 一、基本流程 参考ThreeJs 图形绘制基础[https://www.jianshu.com/p/a8ec586...
    合肥黑阅读 1,564评论 0 0
  • 设计模式(Design pattern)是一套被反复使用、多数人知晓的、经过分类编目的、代码设计经验的总结。 设计...
    Jinbeen阅读 1,817评论 0 6
  • 什么是设计模式 设计模式(Design pattern)是一套被反复使用、多数人知晓的、经过分类编目的、代码设计经...
    辽A丶孙悟空阅读 764评论 1 15
  • 桥接(Bridge)是用于把抽象化与实现化解耦,使得二者可以独立变化。这种类型的设计模式属于结构型模式,它通过提供...
    陈吉思_汗阅读 136评论 0 0
  • 转:http://www.runoob.com/design-pattern/bridge-pattern.htm...
    right_33cb阅读 129评论 0 0

友情链接更多精彩内容