In the CodeGuru forum to see an article on the design of graphics, feel very helpful. Write down the main points in this article to facilitate future viewing.
1. The constructor function of the base class shape class should be protected
Design a common base class shape for all shapes. A specific graphic class, such as Rectangle,circle, derives from the shape class, and the shape's constructor should be protected. That is, the shape class can only serve as a base class, not directly to create Shape objects. The rationale for this is: A. " Shape is an abstract concept that cannot be displayed on the screen without indicating which of the specific shapes it is. This object is meaningful and can be displayed only if it specifies which shape (round or square, etc.). B. Avoid errors that can be caused by creating objects directly. In addition constructors are best initialized with data members such as the color of the shape, linearity, padding mode, and so on.
2. Point information
The data that holds a series of points in the shape class is used for drawing:vector<point> points;
3. Virtual Drawing function
Add function void Draw () const in shape; (The const here shows that the function does not change the value of the data member) the function first processes the color, linearity, and so on, and then invokes the virtual void Draw_lines () const to draw. virtual void Draw_lines () is designed as a virtual function, so subclasses of the shape class need to define how they draw. Some other functions should also be designed as virtual functions, such as Move (). After all, subclasses have a clearer idea of how to draw and move specific graphics.
4. Disable copy constructors and object assignment
Declare a copy constructor and an object assignment operator as private:
Private
Shape (const shape&); Prevent copying
shape& operator= (const shape&);
This is because it is unsafe to copy between the base class and the subclass/between the different child classes. Because they have different members, occupy a different size of space, random copy will cause memory slices (sliced) and other unintended consequences. If you need to use copy functionality, you can design a function clone () to implement it.