In addition to Weibo, there is also WeChat
Please pay attention
WeChat public account
Shulou
2025-01-28 Update From: SLTechnology News&Howtos shulou NAV: SLTechnology News&Howtos > Development >
Share
Shulou(Shulou.com)06/03 Report--
This article introduces the knowledge of "explaining the mechanism of encapsulation and information hiding in C++". Many people will encounter such a dilemma in the operation of actual cases. then let the editor lead you to learn how to deal with these situations. I hope you can read it carefully and be able to achieve something!
Catalogue
Overview
The common interface of the class
Private implementation of the class
Methods and messages
Overview
Encapsulation is a concept in object-oriented programming that binds data to functions that manipulate data. This can avoid external interference and misuse.
Data hiding includes two parts: data encapsulation and data abstraction. Data encapsulation is a mechanism that bundles data with functions that manipulate it. Data abstraction is a mechanism that only exposes interfaces to users and hides specific implementation details.
The common interface of the class
C++ encapsulates data and operations related to it in a class. After declaring a class, the user mainly implements the function provided by the class by calling the common member function, which is called message passing.
The common member function is the common interface (public interface) of the class used by the user, or the external interface of the class. Private data members cannot be accessed directly outside the class, but private data members can be referenced or even modified by calling public member functions.
Private implementation of the class
Users do not need to care about the details of the implementation of these functions, but only need to know which function will get what results and what functions will be achieved. Manipulating data members through member functions is called class implementation. The details of the implementation are hidden from the user. (information hiding)
The data manipulated in the class is private, and the details of the implementation are hidden from the user. This is called a private implementation. (private implementation)
The separation of the public interface of the class from the private implementation forms information concealment. Separating the interface from the implementation is one of the most basic principles of software engineering.
Advantages:
If you want to expand the tired function, you only need to modify the relevant data members in this class and its related member functions. The out-of-class part of the program does not have to be modified
If you find that there are errors in reading and writing data in the class during compilation, you do not have to check the entire program. Just check the few member functions in this class that access this data
Methods and messages
Method (method) means that the member function of a class is called a method in object-oriented programming theory, which refers to the operation of data. A method corresponds to an operation. Only methods that are declared public can be activated by the outside world.
A message refers to a call to a common function. The outside world activates the method by sending a message. Calling the member function of an object is a message sent to the object. Tell it to execute the method.
Object-oriented features:
Encapsulation: relative independence between objects
Abstractness: a class is an abstraction of an object, and an object is an instance of a class
Inheritance: solving the problem of reuse
Polymorphism: related different classes generated by inheritance. Its objects respond differently to the same message.
This is the end of the introduction to the mechanism of encapsulation and information hiding in C++. Thank you for your reading. If you want to know more about the industry, you can follow the website, the editor will output more high-quality practical articles for you!
Welcome to subscribe "Shulou Technology Information " to get latest news, interesting things and hot topics in the IT industry, and controls the hottest and latest Internet news, technology news and IT industry trends.
Views: 0
*The comments in the above article only represent the author's personal views and do not represent the views and positions of this website. If you have more insights, please feel free to contribute and share.
Continue with the installation of the previous hadoop.First, install zookooper1. Decompress zookoope
"Every 5-10 years, there's a rare product, a really special, very unusual product that's the most un
© 2024 shulou.com SLNews company. All rights reserved.