473,503 Members | 1,818 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 1420
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******@hotmail.coma écrit dans le message de news:
%2****************@TK2MSFTNGP04.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, authentification, 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
4102
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...
12
2067
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...
47
3290
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) ...
63
2612
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...
32
4165
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...
11
5622
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...
1
14213
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++ ...
2
7611
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...
162
10065
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...
0
7202
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,...
0
7086
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...
0
7280
Oralloy
by: Oralloy | last post by:
Hello folks, I am unable to find appropriate documentation on the type promotion of bit-fields when using the generalised comparison operator "<=>". The problem is that using the GNU compilers,...
0
7330
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...
0
7460
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...
1
5014
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...
0
3154
by: adsilva | last post by:
A Windows Forms form does not have the event Unload, like VB6. What one acts like?
0
1512
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 ...
0
380
bsmnconsultancy
by: bsmnconsultancy | last post by:
In today's digital era, a well-designed website is crucial for businesses looking to succeed. Whether you're a small business owner or a large corporation in Toronto, having a strong online presence...

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.