-1

Today,i was searching about abstraction and i got this example....how this program implements the concept of abstraction and please also elaborate what is abstraction in c++

 #include <iostream>
using namespace std;

class Adder{
   public:
      // constructor
      Adder(int i = 0)
  {
    total = i;
  }
  // interface to outside world
  void addNum(int number)
  {
      total += number;
  }
  // interface to outside world
  int getTotal()
  {
      return total;
  };
  private:
  // hidden data from outside world
  int total;
};
int main( )
{
Adder a;

a.addNum(10);
a.addNum(20);
a.addNum(30);

cout << "Total " << a.getTotal() <<endl;
return 0;
}
Vik Jamwal
  • 1
  • 1
  • 1
  • You found a program that claimed to implement the "concept of abstraction" and that didn't say anything about it? Where did you find it? How do you know it does implement the "concept of abstraction" if that was not mentioned in the first place? – Andy Prowl Feb 17 '13 at 14:48
  • here is the source [link]http://www.tutorialspoint.com/cplusplus/cpp_data_abstraction.htm[link] – Vik Jamwal Feb 17 '13 at 14:49
  • It looks like there's a whole section of text before the example. What information do you miss or not understand? – Andy Prowl Feb 17 '13 at 14:50
  • i am not able to differentiate between data abstraction and data encapsulation ,both are looking same. – Vik Jamwal Feb 17 '13 at 14:52
  • @vikJ: There's nothing relating the two. Read the material again. – Lightness Races in Orbit Feb 17 '13 at 14:53
  • @vikJ: Then [this link](http://www.tutorialspoint.com/cplusplus/cpp_data_encapsulation.htm) from the very same web page may help – Andy Prowl Feb 17 '13 at 14:53

3 Answers3

1

It should be called data abstraction, which is the key source of OOP(not limited to C++).

Quoted from wikipedia:

Data abstraction enforces a clear separation between the abstract properties of a data type and the concrete details of its implementation.

In your example, Adder is a data abstraction of an adder, which has two interfaces: addNum and getTotal. This abstraction hides(or encapsulates) the private data(total in this case), only expose its kernel behavior: adding a number and returning the current sum.

Hui Zheng
  • 10,084
  • 2
  • 35
  • 40
0

You are accessing the public methods for doing your tasks without knowing how the private members are affected. this is nothing but abstraction .

You are moving your hand without knowing how your mind instruct it to move.

Data Abstraction : Hiding unnecessary details . in your case you hide how the total is calculated. you just call the function and your task is done.

Data Encapsulation: Binding data with object. In your case you binded the total with object a. so it's not accessible without a permission.

Arpit
  • 12,767
  • 3
  • 27
  • 40
0

Think of the constructor and function prototypes only:

class Adder {
  Adder(int i);
  void AddNum(int num);
  int getTotal();
};

The implementation is hidden, abstracted away, and only the prototypes remain.

tp1
  • 1,197
  • 10
  • 17