Advanced thinking on "encapsulation"-snippets

Source: Internet
Author: User

 


MSO-ascii-font-family: "Courier New" '> [easy moments]: BMW and a canvas

MSO-ascii-font-family: "Courier New" '> it's raining. When a BMW was built on the yard, the staff took a canvas and covered it strictly. Ding Xiaoming and miss Nan experienced this scene.

MSO-ascii-font-family: "Courier New" '> Ding Xiaoming: "Look, that canvas is an encapsulation, it protects the bottom of the BMW "Courier New" '> from the wind and rain."

MSO-ascii-font-family: "Courier New" '> Nan Yu: "No, the real encapsulation is BMW. This car is the perfect encapsulation art ".

 

"Courier New"; MSO-Hansi-font-family: "Courier New" '> is there so much content in "encapsulation? Some textbooks do not include "publicmso-Hansi-font-family:" Courier New "'>, protected" Courier New "'>, and private; MSO-ascii-font-family: "Courier New"; MSO-Hansi-font-family: "Courier New" '>, is the content about "encapsulation" ended? Let's review the history of this chapter.

; MSO-ascii-font-family: "Courier New"; MSO-Hansi-font-family: "Courier New" '>

 

Wingdings; font-weight: normal "> ø unchanged is the soul of the class

"Courier New"; MSO-Hansi-font-family: "Courier New" '> understand encapsulation ", the first thing to understand in terms of syntax and semantics is the pointer "thismso-Hansi-font-family:" Courier New "'>, knowing that it is stolen by the compiler will secretly add an input parameter to the member function, that is, it. Then we need to understand that even if it looks like an empty class, it will also have some "default behavior ".

"Courier New"; MSO-Hansi-font-family: "Courier New" '> however, the first thing about encapsulation that has a "soul" is the class's unchanged style ". Let's review these soul-free packages:

Code:
  1. Int X, Y;

 

"Courier New"; MSO-Hansi-font-family: "Courier New" '> is this a coordinate? This is streaking. There is no encapsulation.

Code:
  1. Struct point
  2. {
  3. Int X, Y;
  4. };

"Courier New"; MSO-Hansi-font-family: "Courier New" '>

"Courier New"; MSO-Hansi-font-family: "Courier New" '> the psychological burden of code readers is reduced by the existence of this structure, but it does. "Courier New" '> there is no encapsulation of more information.

Code:
  1. Class Point
  2. {
  3. Public:
  4. Int getx () const {return _ x ;}
  5. Int Gety () const {return _ y ;}
  6. Void setx (int x) {_ x = x ;}
  7. Void sety (INT y) {_ y = y ;}
  8. PRIVATE:
  9. Int _ x, _ y;
  10. };

 

"Courier New"; MSO-Hansi-font-family: "Courier New" '> This is redundancy protection, structmso-Hansi-font-family: "Courier New" '> put on a small vest and shake it into Class "Courier New"'>, but we still recognize it.

"Courier New"; MSO-Hansi-font-family: "Courier New" '> object-oriented technology was originally used to deal with complex enough problems. If you are dealing with a simple thing in a simple program (the coordinate point here), structmso-Hansi-font-family: "Courier New" '> version of Point "Courier New"'> is the best choice. If you are dealing with a simple object in a complex program, the version of redundancy protection is worth learning from.

"Courier New"; MSO-Hansi-font-family: "Courier New" '> A single type may have more than a dozen Member data, but if these member data have nothing to do with each other, it is very likely that it is only suitable for building a structure; or, it only has one internal data, but it needs to switch between multiple States according to certain rules when dealing with the external, this class also needs to be well encapsulated to maintain its "Courier New" '> "unchanged ".

Wingdings; font-weight: normal "> ø do I need to copy the constructor and assign values?

"Courier New"; MSO-Hansi-font-family: "Courier New" '> when a class constructs an object, additional resources need to be allocated, A supporting destructor is usually required to release resources before the object disappears. Of course, it does not mean that resources can only be released during the analysis, but it is usually a good time to ensure that resources are safely recycled.

"Courier New"; MSO-Hansi-font-family: "Courier New" '> if this is the case, it is easy to write constructor and destructor in one place; however, if an object is created in another model, a copy constructor is required. Another thought is that objects can be assigned values to each other, so we need to overload the assignment operator. here we need to put down what we already have and then accept what the other party already has ...... During these processes, you must consider the "Deep copy" or "Shallow copy" issue.

"Courier New"; MSO-Hansi-font-family: "Courier New" '> it's time to make a prompt: not all classes (objects) both require "copy" or "assign value. For example, if you write a window (Windows) "Courier New"; MSO-Hansi-font-family: "Courier New" '> program, there will be many and window classes, these classes construct an actual window as needed, and seldom need to "copy", or make amso-Hansi-font-family: "Courier New" '> window is assigned to B "Courier New"'> window. For example, some container classes we have customized (such as the aforementioned integerlist "Courier New" '>) do not need to copy the entire linked list in practical applications.

"Courier New"; MSO-Hansi-font-family: "Courier New" '> when a class does not require "Courier New"'> "copy constructor" or "mutual assignment, you can directly declare these two functions as private:

Code:
  1. Class fontdialog
  2. {
  3. Public:
  4. Fontdialog (STD: String const & title)
  5. : _ Title (title)
  6. {}
  7. ~ Fontdialog (){}
  8. PRIVATE:
  9. Fontdialog (fontdialog const & O );
  10. Fontdialog & operator = (fontdialog const & O );
  11. STD: String _ title;
  12. };

 

"Courier New"; MSO-Hansi-font-family: "Courier New" '> note: we choose fontdialog "Courier New"; MSO-Hansi-font-family: "Courier New" '>'s copy construction and value assignment operator Declaration are private, which means that the external can no longer call these two functions, since the external will not call these two functions, naturally, we do not need to implement them.

"Courier New"; MSO-Hansi-font-family: "Courier New" '> the following code does not pass Compilation:

Code:
  1. Fontdialog dlga ("select font ...");
  2. Fontdialog dlgb (dlga); // compilation fails because private member functions are called.
  3. Fontdialog dlgc ("");
  4. Dlgc = dlga; // The cause is the same as above.

 


MSO-ascii-font-family: "Courier New" '> tip: Elements in the STL container, which must be "reproducible"

STL containers are stored with "values" by default. In addition, to support addition and sorting, elements often need to be copied between them. Therefore, objects in STL "Courier New" '> standard containers must be copied and assigned values. However, if an object cannot be copied logically and needs to be placed in a container, the most correct way is to save their pointers only in the container (corresponding, by allocating elements in the heap ).

-------------------------------------------------------------------------------

If you want to communicate with me, click the following link to become a friend:
Http://student.csdn.net/invite.php? U= 112600 & C = f635b3cf130f350c

 

 

 

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.