Velocity Reviews - Computer Hardware Reviews

Velocity Reviews > Newsgroups > Programming > C++ > data abstraction vs encapsulation example needed

Reply
Thread Tools

data abstraction vs encapsulation example needed

 
 
subramanian100in@yahoo.com, India
Guest
Posts: n/a
 
      09-15-2007
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

 
Reply With Quote
 
 
 
 
Phlip
Guest
Posts: n/a
 
      09-15-2007
subramanian100in wrote:

> 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++


"Abstraction" means its dictionary definition, which is probably "replace a
complex topic with a simple name, as a convenient reference." That's all it
can mean; there is (thankfully!) no C++ keyword 'abstract'!

Learn the keyword 'virtual', to get with one of C++'s abstracting systems.

"Encapsulation" means each object in a program has a capsule, which is the
set of methods used to operate it. The most trivial example is:

class Foo
{
public:
int getBar();
private:
int Bar;
};

That means the calling code cannot access Bar directly. The class Foo can
therefor maintain Bar, and can ensure that client code always thinks it
contains the correct value. These rules make programs easier to change and
upgrade.

--
Phlip
http://www.oreilly.com/catalog/9780596510657/
"Test Driven Ajax (on Rails)"
assert_xpath, assert_javascript, & assert_ajax


 
Reply With Quote
 
 
 
 
Juha Nieminen
Guest
Posts: n/a
 
      09-15-2007
http://www.velocityreviews.com/forums/(E-Mail Removed), India wrote:
> 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++


Abstraction means basically that you have a type which is documented
to behave in a certain way, but its actual implementation is hidden.
The motive to do this is that you don't write code which assumes that
the type in question is implemented in a certain way. This allows more
easily changing the implementation without breaking existing code.

The simplest possible form of abstraction is aliasing "away" a basic
type. For example the size_t type is a good example of this. You don't
know, and you really don't even want to know what 'size_t' really is
(whether it's an unsigned int, an unsigned long or something else). You
just know that it behaves like an unsigned integral type (it's
documented to behave like one) and you can use it to index arrays and
such. Using size_t for this purpose makes your code more portable
because in different systems size_t may be implemented in different ways
and you haven't hard-coded your program to assume it's always
implemented in the same way.

A more elaborated example of abstraction is std::string. It's a type
which has been documented to behave in certain ways (among other things
by its member functions and their specifications) but you don't really
know nor care how it's internally implemented. This allows compilers to
use different implementations for std::string because your code doesn't
hard-code any specific one.

Another form of abstraction is using constants instead of literals.
For example, RAND_MAX is a good example. Instead of assuming that rand()
returns values between 0 and 32767, you just assume that it returns
values between 0 and RAND_MAX, without specifying in your code what that
maximum is. This allows compilers to change that maximum value as they
please, and your code will not break.

Encapsulation is related to modularity
(http://en.wikipedia.org/wiki/Module_%28programming%29), and it's a tool
for abstraction.

Basically you are grouping a set of related resources (usually
variables) into a single module or class. One of the advantages of this
is that you can then create several independent instances of that class
(ie. you create objects), each with their own set of those resources.

Encapsulation is related to abstraction in that modules/classes
usually have a public interface and a private section. The public
interface is the part that defines how the class behaves, while the
private section contains implementation details, hidden from the outside.
 
Reply With Quote
 
 
 
Reply

Thread Tools

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is On
HTML code is Off
Trackbacks are On
Pingbacks are On
Refbacks are Off


Similar Threads
Thread Thread Starter Forum Replies Last Post
understanding of data abstraction, information hiding and encapsulation subramanian100in@yahoo.com, India C++ 3 09-25-2007 09:40 AM
Best way to do data source abstraction Arthur Pemberton Python 4 06-01-2006 02:03 PM
Data abstraction in C Anand Vardhan C Programming 9 09-01-2005 02:20 AM
Is there a field-data abstraction layer for DBI/MySQL? Ole Perl Misc 4 12-16-2004 10:19 PM
Specific request with code example (encapsulation) __init__ problems Justin Python 4 08-27-2004 12:59 PM



Advertisments