### 解决复杂问题的方法：1.分解 2.抽象

#### 分解

`#include <iostream>#include <vector>using namespace std;/****************************/class Point {public:    int x;    int y;};// 直线class Line{public:    Point start;    Point end;    Line(const Point &start, const Point &end)    {        this->start = start;        this->end = end;    }};// 矩形class Rect{public:    Point leftUp;    int width;    int height;    Rect(const Point& leftUp, int width, int height)    {        this->leftUp = leftUp;        this->width = width;        this->height = height;    }};// 增加一个类class Circle{};/*************************/enum class Shape{    Line,    Rect,    Circle,};class MainForm{public:    MainForm() {}    virtual void OnMouseDown(const int x, const int y)    {        p1.x = x;        p1.y = y;    }    virtual void OnMouseUp(const int x, const int y, Shape shape)    {        p2.x = x;        p2.y = y;        switch (shape)        {        case Shape::Line:        {            lineVector.push_back(Line(p1, p2));        }        break;        case Shape::Rect:        {            rectVector.push_back(Rect(p1, p2.x - p1.x, p2.y - p1.y));        }        break;        // 改变        case Shape::Circle:        {            // ....        }        break;        default:            break;        }    }    virtual void OnPaint()    {//刷新        // 针对直线        for (int i = 0; i < lineVector.size(); i++) {            cout << "直线，起点(" << lineVector[i].start.x << "," << lineVector[i].start.y << ") " <<                "终点(" << lineVector[i].end.x << "," << lineVector[i].end.y << ")" << endl;        }        cout << endl;        // 针对矩形        for (int i = 0; i < rectVector.size(); i++) {            cout << "矩形，起点(" << rectVector[i].leftUp.x << "," << rectVector[i].leftUp.y << ")" <<                "宽度：" << rectVector[i].width << "高度：" << rectVector[i].height << endl;        }        // 改变  针对圆        for (;;) {            // ....            break;        }    }private:    Point p1;    Point p2;    // 数据结构存放    vector<Line> lineVector;    vector<Rect> rectVector;    //改变    vector<Circle> circleVector;};/*********************************/int main(){    MainForm mainForm;    // 画直线    mainForm.OnMouseDown(0, 0);    mainForm.OnMouseUp(100, 100, Shape::Line);    mainForm.OnMouseDown(0, 0);    mainForm.OnMouseUp(110, 110, Shape::Line);    // 画矩形    mainForm.OnMouseDown(10, 10);    mainForm.OnMouseUp(1000, 1000, Shape::Rect);    mainForm.OnMouseDown(100, 100);    mainForm.OnMouseUp(1000, 1000, Shape::Rect);    // 刷新    mainForm.OnPaint();    getchar();    return 0;}`

`直线，起点(0,0) 终点(100,100)直线，起点(0,0) 终点(110,110)矩形，起点(10,10)宽度：990高度：990矩形，起点(100,100)宽度：900高度：900`

#### 抽象

`#include <iostream>#include <vector>#include <math.h>using namespace std;/****************************/class Point {public:    int x;    int y;};// 接口class IShape {public:    virtual void Draw() = 0;    virtual ~IShape() { }};// 直线class Line : public IShape{public:    Point start;    Point end;    Line(const Point &start, const Point &end)    {        this->start = start;        this->end = end;    }    // 自己画自己，类功能，各司其职。    virtual void Draw() override    {        cout << "直线，起点(" << start.x << "," << start.y << ") " <<            "终点(" << end.x << "," << end.y << ")" << endl;    }};// 矩形class Rect : public IShape{public:    Point leftUp;    int width;    int height;    Rect(const Point& leftUp, int width, int height)    {        this->leftUp = leftUp;        this->width = width;        this->height = height;    }    virtual void Draw() override    {        cout << "矩形，起点(" << leftUp.x << "," << leftUp.y << ")" <<            "宽度：" << width << " 高度：" << height << endl;    }};// 增加一个类class Circle : public IShape{public:    Point m_center;    int R;    Circle(const Point &center, int r) :m_center(center), R(r) {}    virtual void Draw() override    {        cout << "圆形，圆心(" << m_center.x << "," << m_center.y << ")" <<            "半径:" << R << endl;    }};/*************************/enum class ShapeType{    Line,    Rect,    Circle,};class MainForm{public:    MainForm() {}    virtual ~MainForm()    {        vector<IShape *>::iterator itor = m_ShapeVector.begin();        while (itor != m_ShapeVector.end()) {            delete (*itor);            *itor = nullptr;            itor = m_ShapeVector.erase(itor);            itor++;        }    }    virtual void OnMouseDown(const int x, const int y)    {        p1.x = x;        p1.y = y;    }    virtual void OnMouseUp(const int x, const int y, ShapeType shape)    {        p2.x = x;        p2.y = y;        switch (shape)        {        case ShapeType::Line:        {            m_ShapeVector.push_back(new Line(p1, p2));        }        break;        case ShapeType::Rect:        {            m_ShapeVector.push_back(new Rect(p1, p2.x - p1.x, p2.y - p1.y));        }        break;        // 改变     后面这里用工厂模式，都不用修改        case ShapeType::Circle:        {            double r = sqrt((p2.x - p1.x) *  (p2.x - p1.x) + (p2.y - p1.y) * (p2.y - p1.y));            m_ShapeVector.push_back(new Circle(p1, static_cast<int>(r)));        }        break;        default:            break;        }    }    virtual void OnPaint()    {//刷新        for (int i = 0; i < m_ShapeVector.size(); i++) {            m_ShapeVector[i]->Draw(); // 多态调用        }    }private:    Point p1;    Point p2;    // 数据结构存放    vector<IShape *> m_ShapeVector;};/*********************************/int main(){    MainForm mainForm;    // 画直线    mainForm.OnMouseDown(0, 0);    mainForm.OnMouseUp(100, 100, ShapeType::Line);    mainForm.OnMouseDown(0, 0);    mainForm.OnMouseUp(110, 110, ShapeType::Line);    // 画矩形    mainForm.OnMouseDown(10, 10);    mainForm.OnMouseUp(1000, 1000, ShapeType::Rect);    mainForm.OnMouseDown(100, 100);    mainForm.OnMouseUp(1000, 1000, ShapeType::Rect);    // 画圆    mainForm.OnMouseDown(100, 100);    mainForm.OnMouseUp(1000, 1000, ShapeType::Circle);    mainForm.OnMouseDown(101, 101);    mainForm.OnMouseUp(1100, 1100, ShapeType::Circle);    // 刷新    mainForm.OnPaint();    getchar();    return 0;}`

`直线，起点(0,0) 终点(100,100)直线，起点(0,0) 终点(110,110)矩形，起点(10,10)宽度：990 高度：990矩形，起点(100,100)宽度：900 高度：900圆形，圆心(100,100)半径:1272圆形，圆心(101,101)半径:1412`