what is data Abstraction? and give example
Answer Posted / sankalp
abstraction - it is the act of representing the essential features without including the background details.
classes uses the concept of abstraction.
Benefits of Data Abstraction:
Data abstraction provide two important advantages:
Class internals are protected from inadvertent user-level errors, which might corrupt the state of the object.
The class implementation may evolve over time in response to changing requirements or bug reports without requiring change in user-level code.
By defining data members only in the private section of the class, the class author is free to make changes in the data. If the implementation changes, only the class code needs to be examined to see what affect the change may have. If data are public, then any function that directly accesses the data members of the old representation might be broken.
Data Abstraction Example:
Any C++ program where you implement a class with public and private members is an example of data abstraction. Consider the following example:
#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;
}
When the above code is compiled and executed, it produces following result:
Total 60
| Is This Answer Correct ? | 1 Yes | 0 No |
Post New Answer View All Answers
Why main function is special in c++?
Which programming language is best?
what is COPY CONSTRUCTOR and what is it used for?
What does it mean to declare a member variable as static?
What are friend functions in C++?
Search for: what is pair in c++?
How do I tokenize a string in c++?
Give 10 points of differences between C & C++.
What is the basic structure of a c++ program?
What are its advantages and disadvantages of multiple inheritances (virtual inheritance)?
Can constructor be static in c++?
Why is it necessary to use a reference in the argument to the copy constructor?
To which numbering system can the binary number 1101100100111100 be easily converted to?
What are the 3 levels of programming languages?
What is the difference between #import and #include in c++?