Graphic Class Design

Source: Internet
Author: User

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.

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.