data abstraction vs encapsulation example needed

S

subramanian100in

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
 
P

Phlip

subramanian100in said:
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.
 
J

Juha Nieminen

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_(programming)), 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.
 

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

Forum statistics

Threads
473,769
Messages
2,569,579
Members
45,053
Latest member
BrodieSola

Latest Threads

Top