classDot: public Graphic { public: int x_; int y_; Dot(int x, int y): x_(x), y_(y) {} voidmove(int x, int y){ x_ += x; y_ += y; } voiddraw(Canvas& c){ // draw dot on the canvas } };
classCircle: public Dot { public: int radius_; Circle(int x, int y, int r): Dot(x, y), radius_(r) {} voiddraw(Canvas& c){ // draw a circle on the canvas } };
classCompoundGraphic: public Graphic { public: std::vector<Graphic*> children; voidadd(Graphic* child){ children.push_back(child); } voidremove(Graphic* child){ // delete from children } voidmove(int x, int y){ for(int i = 0; i < children.size(); i++) { children[i]->move(x, y); } } voiddraw(Canvas& c){ // for each child component, draw the component // also draw the bounding boxes } };
classEditor { public: CompoundGraphic all_; voidload(){ all_ = CompoundGraphic(); all_.add(newDot(1, 2)); all_.add(newCircle(1, 2, 3)); // ... } voidgroupSelected(std::vector<Graphic*>& arr){ CompoundGraphic* group = newCompoundGraphic(); for (int i = 0; i < arr.size(); i++) { group.add(arr[i]); all_.remove(arr[i]); } all_.add(group); all_.draw(); } };