What is encapsulation?

E

E. Robert Tisdale

Marshall Cline wrote:

http://www.parashift.com/c++-faq-lite/classes-and-objects.html#faq-7.4

"Preventing unauthorized access
to some piece of information or functionality."

That's *data hiding* -- not encapsulation.
A C struct *encapsulates* data members
but C does not support any mechanism
for hiding the actual data representation
except *opaque* data structures.

"Inheritance can also be used as a form of encapsulation."

Yes, but public inheritance does not "hide" the base class.

"Designing a clean interface
and separating that interface from its implementation
merely allows users to use the interface.
But encapsulating (putting "in a capsule") the implementation
forces users to use the interface."

No. It does *not* force the "user" to use the interface.
A determined hacker will find a way to "reach around" the interface
and access the data members directly.
Data hiding is *not* encryption.
It merely prevents unwary application programmers
from *accidently* accessing data members directly.
 
J

John Fullman

Ohhh... a philisophical question.

Encapsulation is probably best defined as one of the quotes above that
said:
(putting "in a capsule") <--- I like that :)

It's not hiding things from the developer (although that can be part of
it)... and it's not forcing people to use an interface (it's allowing
people to use an interface)

Ultimatley, it's taking an idea, task, or entity and packing it up in
code to make coding other things easier.
 
P

Peter Jansson

E. Robert Tisdale said:
No. It does *not* force the "user" to use the interface.
A determined hacker will find a way to "reach around" the interface
and access the data members directly.

Could you please give an example of how a determined coder may "reach
around" the interface?

Regards,
Peter Jansson
 
M

Mogens Heller Jensen

mlimber said:
#define private public

If you can do that, it means you have access to the source code.... then you
might as well dive in and change the whole thing :eek:) or did I miss some
crucial point here?
 
J

John Fullman

All you need is access to the header files, not the full source, to do
that.
And any application that wants someone to use an interface has to fork
over the header files.
Otherwise, the interface won't be defined.
 
P

Peter Jansson

mlimber said:
#define private public

Can this be prevented if the interface code contains the following?

#ifdef private
#undef private
#endif

Regards,
Peter Jansson
 
J

John Fullman

It could, but it would be just as easy to remove that protection code
from any header file.
That would only work if the header files were somehow write protected.
 
P

Peter Jansson

John said:
It could, but it would be just as easy to remove that protection code
from any header file.
That would only work if the header files were somehow write protected.
I agree.

I believe this to be of little importance since the "attacker" still has
access to the headers so that person could easily change private into
public anyway.

Regards,
Peter
 
E

E. Robert Tisdale

Peter said:
I agree.

I believe this to be of little importance since the "attacker"

You are confused about the purpose of data hiding.
Reaching around the interface cannot be considered an "attack"
on the class library or anything else
because it isn't possible for application programmers
to injure anyone but themselves by doing so.
Data hiding (private data members) are more like a "safety handrail"
that helps prevent application programmers for *accidently*
accessing private data members directly.
Abrogating this safety mechanism may be evidence of stupidity
but *not* malice.
 
P

Puppet_Sock

Encapsulation is: When you work on a jigsaw puzzle, the
puzzle starts as a pile-o-parts in a big plastic bag.
The bag is encapsulation. As long as nobody gets "frisky"
the bag will only have parts from this puzzle.

Data hiding is if the bag is opaque and you have to reach
in to get parts. This means you can't see if you are grabbing
an edge part or a corner part or a middle part, nor what
colour it is.

Implementation hiding is if the entire bag is behind a barrier,
and to get another part you have to ask your mom. So you
don't even see the bag, nor know how full the bag still is,
nor even that there *is* a bag. Could be a box or one of
those "bingo" machine air-popup things or your cat could
be behind there dragging over another part every time your
mom holds out a cat treat for it. But your mom told you she
will give you another part whenever you ask for one, until
there are no more.
Socks
 
J

John Fullman

Hehe... Totally agree, unless the writer of the library left security
holes in private members.
But, more often than not, you are right... that is just shooting
yourself in the foot.
 
H

Howard

John Fullman said:
Hehe... Totally agree, unless the writer of the library left security
holes in private members.
But, more often than not, you are right... that is just shooting
yourself in the foot.

How about quoting what you're responding to? This isn't a chat room.

-Howard
 
J

John Fullman

Howard said:
How about quoting what you're responding to? This isn't a chat room.

-Howard

Good point... but up until 30 seconds ago, I didn't know how to quote
someone. They had to go hide it on me. :p

As for my previous post, I was replying to the post above mine. You can
also switch to a tree view to see which message I was replying to.
 
S

Sherm Pendley

John Fullman said:
As for my previous post, I was replying to the post above mine.

In my newsreader, there is no post above yours. We're not all (ab)using a
web browser to access Usenet, you know.
You can also switch to a tree view

How would you know if my news reader has a tree view or not, or whether my
computer has a large enough screen to support such a layout? How would you
know if the server I'm connected to has received the post you're replying
to, or if it still retains it?

sherm--
 
P

persenaama

Data hiding (private data members) are more like a "safety handrail"
that helps prevent application programmers for *accidently*
accessing private data members directly.

Nonsense! The standard doesn't define accidents, only the correct
semantics of the c++ programming language. Why speculate on different
uses this language feature might or might not be put into? The
definition is pretty straightword, that's all there is to it. Why
complicate simple things?

Furthermore, abrogating this safety mechanism may invoke undefined
behaviour depending on how the mechanism was abrogated, and what's the
practical difference between stupidity and malice as far as the
correctness of the program is concerned? None, if the program invokes
undefined behaviour the only important question is how to fix it unless
interested in finding who to blame or other off-topic activities.

If the source is modified, well, then it reasonably follows that we are
not talking about the original source at that point and the question is
what the new one looks like. It might be a brilliant design change, not
evidence of stupidity or it might invoke undefined behaviour. The point
is that we don't know, why should anyone pretend that he/she does?
 
U

u.int.32.t

Sherm said:
In my newsreader, there is no post above yours. We're not all (ab)using a
web browser to access Usenet, you know.


How would you know if my news reader has a tree view or not, or whether my
computer has a large enough screen to support such a layout? How would you
know if the server I'm connected to has received the post you're replying
to, or if it still retains it?

Spoken like a true C++ programmer.
 
J

jimjim

Sherm Pendley said:
In my newsreader, there is no post above yours. We're not all (ab)using a
web browser to access Usenet, you know.
Which newsreader are you using? Do you use a PDA?
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

No members online now.

Forum statistics

Threads
473,774
Messages
2,569,598
Members
45,144
Latest member
KetoBaseReviews
Top