473,654 Members | 3,072 Online
Bytes | Software Development & Data Engineering Community
+ Post

Home Posts Topics Members FAQ

Data Encapsulation?

Neo
I was written this code; in VS2k5

class Program

{

private: int a;

private: int b;

private: char c;

public: Program( ) : a(10), b(20), c('Y'){ }

};

int _tmain(int argc, _TCHAR* argv[])

{

A obj;

int temp = *((int *)&obj + 1);

char ch = *((char *)&obj + 8);

cout<< temp<<endl<<ch< <endl;

_gettch( );

return 0;

}

in Object Orient World we learn Data Encapsulation or Data Hiding for data
security.

In this code, what is the meaning of Data Encapsulation or Data Hiding for
data security?

Regards,

-neo
Jul 19 '06 #1
2 1435
Neo schrieb:
in Object Orient World we learn Data Encapsulation or Data Hiding for data
security.

In this code, what is the meaning of Data Encapsulation or Data Hiding for
data security?
In this code the class members are hidden and not visible to non-friends.

If you would try to access the members the correct way:

e.g. obj.a = 10;

you will get a compiler error. This would be the correct behaviour, but
you are using a bad hack. Of course your code *can* work, but it has not
to work. If you have vtable this can result in undefined behaviour.

There is no security of accessing if you access elements in this way.
And there can't be such a security.

Regards,
Vinzenz
Jul 19 '06 #2

"Neo" <mo******@hotma il.coma écrit dans le message de news:
%2************* ***@TK2MSFTNGP0 4.phx.gbl...
>I was written this code; in VS2k5
<snip>>
A obj;

int temp = *((int *)&obj + 1);
char ch = *((char *)&obj + 8);
cout<< temp<<endl<<ch< <endl;

in Object Orient World we learn Data Encapsulation or Data Hiding for data
security.

In this code, what is the meaning of Data Encapsulation or Data Hiding for
data security?
The aim of data access restriction (protected, private) in OO language is to
protect against Murphy - that is the programmer inadvertently
reading/writing a variable it shouldn't be interested in.
It is not aimed at protecting against Machiavel - that is a hacker who want
to acess data that should be forbidden to it. If you nee that kind of
protection, you probably need cryptography, authentificatio n, access
control, etc...
Anyway, as Vinzenz has said, your "solution" is a hack that depends on
knowledge of the compiler internals working (ie, data padding and
alignement, presence of a v-ptr, data member reorganization, etc...)

Note that this is true in all OO languages, it is not specific to C++.

Arnaud
MVP - VC
Jul 20 '06 #3

This thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

6
4107
by: blueblueblue2005 | last post by:
here is a friend function of Class Array, which has two private data member: int size, int *ptr // Array's public member function to return size int getSize() const { return size; } friend istream &operator>>(istream &in, Array &a) { for(int i=0; i<a.size; i++) // do something
12
2081
by: Alex Hunsley | last post by:
There's no really specific questions in this post, but I'm looking for people's thought on the issues within... The two main versions I've encountered for data pseudo-hiding (encapsulation) in python are: method 1: _X - (single underscore) - just cosmetic, a convention to let someone
47
3332
by: Roger Lakner | last post by:
I often see operator implemented something like this: class Foo { ... }; class FooList { public: const Foo& operator (unsigned index) const {return array;}; Foo& operator (unsigned index) {return
63
2649
by: time.swift | last post by:
Coming from a C++ / C# background, the lack of emphasis on private data seems weird to me. I've often found wrapping private data useful to prevent bugs and enforce error checking.. It appears to me (perhaps wrongly) that Python prefers to leave class data public. What is the logic behind that choice? Thanks any insight.
32
4200
by: bluejack | last post by:
Ahoy: For as long as I've been using C, I've vacillated on the optimal degree of encapsulation in my designs. At a minimum, I aggregate data and code that operate on that data into classlike files; but now and then I go on an opaque type joyride, and create minimalist header files that define very clean interfaces. The problem with that is that it prevents some optimizations:
11
5641
by: sofeng | last post by:
I'm not sure if "data hiding" is the correct term, but I'm trying to emulate this object-oriented technique. I know C++ probably provides much more than my example, but I'd just like some feedback to find out if I've done anything wrong. Also, I am working on this for an embedded environment, so if there are great inefficiencies with this code, I'd like to know that also. I realize there is overhead with using an "accessor" function. ...
1
14220
by: subramanian100in | last post by:
I am a beginner in C++. I come across the terms data abstraction and encapsulation in C++. I am unable to understand the definitions. Kindly explain these terms with a simple example in C++ Thanks V.Subramanian
2
7627
by: subramanian100in | last post by:
Is my following understanding correct ? Data abstraction means providing the interface - that is, the set of functions that can be called by the user of a class. Information hiding means mentioning the class members(functions, typedefs, data) under the access control labels : public, protected, private. Encapsulation means providing the implementation of class member
162
10216
by: Sh4wn | last post by:
Hi, first, python is one of my fav languages, and i'll definitely keep developing with it. But, there's 1 one thing what I -really- miss: data hiding. I know member vars are private when you prefix them with 2 underscores, but I hate prefixing my vars, I'd rather add a keyword before it. Python advertises himself as a full OOP language, but why does it miss one of the basic principles of OOP? Will it ever be added to python?
0
8372
marktang
by: marktang | last post by:
ONU (Optical Network Unit) is one of the key components for providing high-speed Internet services. Its primary function is to act as an endpoint device located at the user's premises. However, people are often confused as to whether an ONU can Work As a Router. In this blog post, we’ll explore What is ONU, What Is Router, ONU & Router’s main usage, and What is the difference between ONU and Router. Let’s take a closer look ! Part I. Meaning of...
0
8285
by: Hystou | last post by:
Most computers default to English, but sometimes we require a different language, especially when relocating. Forgot to request a specific language before your computer shipped? No problem! You can effortlessly switch the default language on Windows 10 without reinstalling. I'll walk you through it. First, let's disable language synchronization. With a Microsoft account, language settings sync across devices. To prevent any complications,...
0
8706
jinu1996
by: jinu1996 | last post by:
In today's digital age, having a compelling online presence is paramount for businesses aiming to thrive in a competitive landscape. At the heart of this digital strategy lies an intricately woven tapestry of website design and digital marketing. It's not merely about having a website; it's about crafting an immersive digital experience that captivates audiences and drives business growth. The Art of Business Website Design Your website is...
1
8475
by: Hystou | last post by:
Overview: Windows 11 and 10 have less user interface control over operating system update behaviour than previous versions of Windows. In Windows 11 and 10, there is no way to turn off the Windows Update option using the Control Panel or Settings app; it automatically checks for updates and installs any it finds, whether you like it or not. For most users, this new feature is actually very convenient. If you want to control the update process,...
0
8591
tracyyun
by: tracyyun | last post by:
Dear forum friends, With the development of smart home technology, a variety of wireless communication protocols have appeared on the market, such as Zigbee, Z-Wave, Wi-Fi, Bluetooth, etc. Each protocol has its own unique characteristics and advantages, but as a user who is planning to build a smart home system, I am a bit confused by the choice of these technologies. I'm particularly interested in Zigbee because I've heard it does some...
1
6160
isladogs
by: isladogs | last post by:
The next Access Europe User Group meeting will be on Wednesday 1 May 2024 starting at 18:00 UK time (6PM UTC+1) and finishing by 19:30 (7.30PM). In this session, we are pleased to welcome a new presenter, Adolph Dupré who will be discussing some powerful techniques for using class modules. He will explain when you may want to use classes instead of User Defined Types (UDT). For example, to manage the data in unbound forms. Adolph will...
0
5621
by: conductexam | last post by:
I have .net C# application in which I am extracting data from word file and save it in database particularly. To store word all data as it is I am converting the whole word file firstly in HTML and then checking html paragraph one by one. At the time of converting from word file to html my equations which are in the word document file was convert into image. Globals.ThisAddIn.Application.ActiveDocument.Select();...
0
4293
by: adsilva | last post by:
A Windows Forms form does not have the event Unload, like VB6. What one acts like?
1
2709
by: 6302768590 | last post by:
Hai team i want code for transfer the data from one system to another through IP address by using C# our system has to for every 5mins then we have to update the data what the data is updated we have to send another system

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.