数据隐藏、数据封装?实际应用

发布于 2024-10-30 15:34:31 字数 399 浏览 3 评论 0原文

您好,我是 C++ 的新手,正在阅读 C++ 概念............并且我无法理解数据隐藏的实际或实时使用方式。

问题:据我所知,数据隐藏将在 C++ 中实现,以隐藏正在使用的实际数据 在程序的幕后假设通过在类中声明一些数据私有。 当制作并编译 C++ 程序时....考虑某个游戏的示例, 可执行文件(.exe 或 unix 中的其他可执行文件)将提供给用户?

那么我对此感到困惑吗? 现在我的数据隐藏在哪里?当用户使用可执行文件(.exe)时,因为我认为用户无论如何都无法读取或打开.exe来查看文件的内容。 即使我将所有变量声明为公共...数据仍然是隐藏的,因为用户只有 .exe 文件,用户无法读取或操作该文件?

请建议大家?对我认为错误的地方提出一些反馈

Hi I am a newbie to C++ reading the C++ concepts...............and I am unable to comprehend the data hiding in the sense how it would be used practically or in real time.

question : As I know data hiding would implemented in C++ to hide the actual data being used
behind the scenes in a programm suppose by declaring some datas private in a class.
when a C++ program is made and compiled ....consider an example of of some game,
the executable file (.exe or nay other executale in unix) would be given to the user?

so here what I am confused about?
now where is my data hiding?? when the user is using the executable(.exe) because I my opinion .exe cannot be read or opened by the user to see the contents of the file anyway..
even if I declare all my variables public...still the data is hidden because the user just has the .exe file, which can't be read or manipulated by the user?

Please suggest guys?? give some feedback where I am thinking wrong

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(8

口干舌燥 2024-11-06 15:34:31

数据隐藏是一种控制应用程序一部分中的代码对应用程序另一部分中的代码的依赖性的方法。也就是说,当一位代码发生更改时,其他一位或多位代码是否也需要更新?您尽可能希望答案为“否”,因为否则您对程序所做的每一个微小更改都会在程序进入稳定状态并再次正常运行之前引发大量后续更改。为了阻止程序员使他们的代码任意依赖于类实现的任何方面,并鼓励他们仅依赖类对象为客户端执行其预期功能所需的可控部分,您可以指定数据和/或允许客户端使用的功能:这是“公共”和/或“受保护”接口。您希望能够在无需客户端代码关心的情况下进行更改的其他内容应设为私有

这都是关于开发的,与程序在运行时处理的数据无关,也与保护程序逻辑/数据不被最终用户窥探无关

例如,taskinoor 适当地引用了一个 Date 类,该类确保存储的日期保持有效,以便客户端代码不能直接访问并将日期设置为“31 Feb”或其他一些无意义的值。此外,在不影响客户端的情况下,可以将数据布局从使用三个数字存储日、月和年更改为一个数字存储自某个参考日以来的天数,而不影响客户端(产生不同的性能配置文件 - 通过日期更快的相对移动)但与 D/M/Y 值的转换速度较慢)。

Data hiding is a way of controlling dependency of code in one part of an application on code in another part of the application. That is, when one bit of code changes, should one or more other bits of code consequently need updating too? As much as possible, you want the answer to that to be "no", because otherwise every little change you make to your program kicks off an avalanche of consequent changes before the program settles into a stable state where it will again operate properly. To discourage a programmer from making their code arbitrarily dependent on any aspects of your class's implementation, and encourage them to only rely on a controllable part necessary for objects of the class to perform their intended function for the client, you can nominate the data and/or functions that the client is allowed to use: that's the "public" and/or "protected" interface. Other things that you want to be able to change without the client code needing to care should be made private.

This is all about development, and has nothing to do with the data the program handles at run-time, or protecting the program logic/data from snooping on by the end user.

As an example, taskinoor appropriately cites a Date class that ensures the dates stored remain valid, so that client code can't just reach in and set the date to "31 Feb" or some other non-sensical value. Further, without affecting clients, it is possible to change the data layout from say having three numbers store day, month and year to one number storing days since some reference day without affecting the clients (yielding a different performance profile - faster relative movements through dates but slower conversions to/from D/M/Y values).

折戟 2024-11-06 15:34:31

什么是数据封装?
数据封装将数据和对数据进行操作的函数绑定在一个单元中。
如何实现数据封装?
通过创建一个类型。例如:类、结构等。

什么是数据抽象?
数据抽象正在向外部世界隐藏类型的复杂性。
如何实现数据抽象?
通过使用访问说明符。例如:公共、私有、受保护。

访问说明符向谁隐藏数据?
访问说明符向类/结构的对象的用户隐藏数据(通过不向他们提供对数据的直接访问)。

将游戏中的类视为一个单独的单元,其他功能(源代码)使用它来执行某些功能。通常,类具有状态和对状态执行操作的方法。访问说明符对其他源代码(可能是类,也可能是结构)隐藏了类的成员。其他代码可以创建类的对象,并可以通过类公开的公共方法修改类数据成员内的状态存储。因此,访问说明符提供了一种访问类成员的有组织的方式。

What is Data Encapsulation?
Data Encapsulation is binding the data and the functions that operate on the data in a single unit.
How do u implement Data Encapsulation?
By creating a Type. ex: Class, Structure etc.

What is Data Abstraction?
Data Abstraction is hiding the complexities of your type from the external world.
How do you implement Data Abstraction?
By using Access Specifiers. Ex: Public, Private, Protected.

Whom do the Access Specifiers hide data from?
The Access specifiers hide the data(by not providing them direct access to the data) from the users of the objects of the class/structure.

Consider your class in the Game as an separate unit, Which is used by the other functionality(source code) to perform some functionality. Usually, A class has a state and methods which perform operation on the state. The access specifiers hide members of the class from other source code (maybe classes or maybe structures). The other code can create objects of your class and can modify the state stores inside class data members through the public methods exposed by your class. Thus the Access specifiers provide a organised way of accessing class members.

浮云落日 2024-11-06 15:34:31

举一个简单的例子:

有一个ArrayList,它有一个成员--listSize表示它的大小。如果没有数据封装或数据隐藏,此 ArrayList 的用户可以将其设置为除元素数量之外的任何值。
完全是一场灾难。

Just give a simple example:

There's an ArrayList, which has a member--listSize denonting its size. Without Data Encapsulation or Data Hiding, a user of this ArrayList can set it to any value other than the number of elements.
Totally a disater.

云巢 2024-11-06 15:34:31

数据隐藏是指当某人使用链接/使用您的代码的代码时隐藏数据。它用作一种安全机制,以确保对象始终正确构造。它还用作用户处理接口的抽象方法,但可以“随意”更改底层实现,而无需重新编写使用该代码的任何代码(例如,重命名字段名称或删除它)完全地)。

示例:

日期类可以保存月、年和月中的日。根据月份的不同,该月的有效天数也不同(有些是 30 天,有些是 31 天,二月是 28/29)。此外,只有一定数量的有效月份(可以使用枚举来处理)。如果您允许用户直接修改月份中的日期字段,则无法确保该对象始终保存有效数据。数据隐藏用于防止用户直接访问该数据,用户使用访问器方法来获取/修改该字段。这些方法可以有代码来确保只输入有效的数据。

现在,如果稍后决定将日期表示为自 2010 年 1 月 1 日起的天数会更好。用户仍然可以使用先前定义的访问器方法,这将适当地转换输入/输出。

Data hiding refers to hiding data when someone's working with code which links/uses your code. It's used as a safety mechanism to ensure that the object is always constructed properly. It is also used as an abstraction method where users deal with the interface, but the underlying implementation can be changed "at will" without having to re-write any code which uses that code (for example, renaming a field name, or removing it completely).

Example:

A date class can hold the month, year, and day of the month. Depending on the month, there are different valid days of the month (30 for some, 31 for others, 28/29 for February). Also, there's only a certain number of valid months (could be taken care of using an enum). If you allowed a user to directly modify the day of the month field, there's no way to ensure that the object always holds valid data. Data hiding is used to prevent the user from directly accessing this data, and the user uses accessor methods to get/modify the field. These methods could have code to ensure that only valid data is entered.

Now if at a later date it's decided that representing the date as the number of days from January 1, 2010 is better. The user can still use the accessor methods as previously defined, which would translate the inputs/outputs appropriately.

撞了怀 2024-11-06 15:34:31

数据隐藏更多地表现为向其他程序员隐藏实现。

简单的例子:

你有一个 Car 类,其中包含一个公共方法 Drive
和属性 DieselEngine,因为您不希望用户知道您将 DieselEngine 设为私有的所关心的引擎。稍后您可以将引擎替换为 PetrolEngine,但由于用户只能使用 Drive,因此当您向他提供更新时,他不必更改其代码。

Data hiding is more in the form of hiding implementation from other programmers.

Simple example:

you have a class Car which contains a public method Drive
and a property DieselEngine, since you don't want the user to know about what engine is in the care you make DieselEngine private. Later you may replace the engine with a PetrolEngine else but since the user only can use Drive he doesn't have to change his code when you give him an update.

铃予 2024-11-06 15:34:31

您对数据隐藏一词的理解是错误的。它不是对用户隐藏,而是对其他程序员隐藏。

例如,您正在使用另一个程序员开发的 Stack 类。你有方法来推动和弹出价值。但你不知道堆栈实际上是如何实现的。它可以使用数组,也可以使用链表来存储值。但作为该类的用户,您并不关心这些细节。你们都知道您可以向其中推送和弹出。这里堆栈的内部数据对你是隐藏的。维护内部存储是堆栈开发人员的责任,而不是您在使用内部存储时的责任。

You are getting the term data hiding wrong. It is not hiding from the user, it means hiding from the other programmers.

For example, you are using a Stack class developed by another programmer. You have methods to push and pop value. But you don't know how the stack is actually implemented. It could have used an array, or it could have used a linked list to store the values. But you don't care about that details as a user of the class. You all know that you can push and pop to and from it. Here the internal data of stack is hidden from you. It's the responsibly of the developer of stack to maintain the internal storage, not yours when you are using that.

紫罗兰の梦幻 2024-11-06 15:34:31

您需要分配的内容比任何人在这个简短的论坛中输入的内容都要多。我建议这本书:

设计模式解释:面向对象设计的新视角(第二版本)

它将解释面向对象设计的各种元素的目的是什么。例如,什么是封装以及为什么使用它?您实际上封装的是什么?

You need allot more than anyone can type in this short forum. I would suggest the book:

Design Patterns Explained: A New Perspective on Object-Oriented Design (2nd Edition)

It'll explain what the purpose of various elements of Object Oriented design are. For instance, what is encapsulation and why do you use it? And what is it you're actually encapsulating?

后eg是否自 2024-11-06 15:34:31

看一下任何重要数据结构的实现,例如 std::vectorstd::map,您会立即发现了解数据隐藏的好处:客户端不必知道这些数据结构在幕后是如何实现的,他可以简单地使用它们。

Take a look at the implementation of any non-trivial data structure such as std::vector<T> or std::map<K, V> and you will immediately understand the benefit of data hiding: the client does not have to know how these data structures are implemented under the hood, he can simply use them.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文