data abstraction vs encapsulation example needed

Discussion in 'C++' started by subramanian100in@yahoo.com, India, Sep 15, 2007.

  1. , India

    , India Guest

    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
    , India, Sep 15, 2007
    #1
    1. Advertising

  2. , India

    Phlip Guest

    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
    Phlip, Sep 15, 2007
    #2
    1. Advertising

  3. , 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_(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.
    Juha Nieminen, Sep 15, 2007
    #3
    1. Advertising

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

It takes just 2 minutes to sign up (and it's free!). Just click the sign up button to choose a username and then you can ask your own questions on the forum.
Similar Threads
  1. Justin
    Replies:
    4
    Views:
    315
    Michael Fuhr
    Aug 27, 2004
  2. Arthur Pemberton

    Best way to do data source abstraction

    Arthur Pemberton, Jun 1, 2006, in forum: Python
    Replies:
    4
    Views:
    385
    Larry Bates
    Jun 1, 2006
  3. Anand Vardhan

    Data abstraction in C

    Anand Vardhan, Aug 31, 2005, in forum: C Programming
    Replies:
    9
    Views:
    751
    Richard Heathfield
    Sep 1, 2005
  4. , India
    Replies:
    3
    Views:
    640
    Saeed Amrollahi
    Sep 25, 2007
  5. Ole
    Replies:
    4
    Views:
    173
Loading...

Share This Page