Meaning and meaning of encapsulation

Source: Internet
Author: User
I have always spared no effort to recommend you to go to chapter 16, 17, and 18 of <predictional C ++ style>.
The reason is that I have seen many Article Private, encapsulation, polymorphism, and inheritance
Chew repeatedly
I read it again today
Extract a few key words and share them with you.

1. What does encapsulation mean?
En-cap-su-late: surrounded, packaged, or protected by a capsule

2. A good class interface can hide the internal implementation of its own class, and provide a "interface" that is separated and differentiated from the internal implementation of the class"
The external surface of bacterial cell membrane is perceived, exposed, and interacted with the outside world.

3. A good class interface design must be complete without exposing any internal implementation of the class.
If the cell membrane of a bacteria is not closed, the bacteria will certainly not survive. The organic tissue inside the bacteria will quickly overflow and it will certainly die.

4. A good class interface protects the internal implementation of the class to avoid unauthorized access and operations.
The main means to kill bacteria (and humans) is to make things that can damage cell membranes. at the microscopic level, this includes chemicals, enzymes, or organic organisms that can properly create holes (perhaps ultimately nano robots ). at the macro level, knives and guns are the weapons that humans have been keen on for a long time.

5. encapsulation is the core concept of object-oriented programming.

6. The so-called object-oriented interface is usually defined as a combination of data and functions that operate on the Data. Supplement: The interface composed of a group of functions callsCodeIsolated from internal data (the interface is responsible for manipulating internal data ). this concept emphasizes two points. One point is low coupling, and the other is that the combined functions constitute a protective interface.

7. Simply put, object-oriented is the methodology for sharing interfaces and implementations, but such separation must improve high cohesion and low coupling.

8. Except for struct, data members should always be private

9. Public Data violates the encapsulation principle because it allows the calling code to directly manipulate the internal information of the object, which means high trust in the calling code.

10. in reality, few people need to directly manipulate the internal implementation of objects. For example, few people need to directly manipulate my stomach, because that makes it easy for them to do something wrong accidentally. at most, they only use my public interfaces to indirectly manipulate my internal systems. The advantage of this approach is that they must first let me know and get my approval before performing operations, for example, if you hand me a bottle of liquid marked with "Drop me", you can decide whether to use it to wash your hair or wash your car, depending on your own feeling and judgment. of course, some people do have the right to operate my "internal", such as a surgeon, but even so:. this situation is rare. B. at least I can choose whether I want to operate C. at least I can choose a highly trusted doctor. the above is the legal scope, otherwise it will be violent. as rude as reinterpret_cast, it will not give a good impression or bring good results.

I thought that all the above 9 points were foreplays. at, I reached a climax. I joined my new insights slightly at the end. I hope you will understand it after reading it, high ~

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.