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
Evaulate: 22%5 a) 2 b) 4 c) 0
Define a pointer to a data member of the type pointer to pointer?
Why c++ is faster than java?
Mention the purpose of istream class?
What is class and structure in c++?
What is a stack c++?
Is swift better than c++?
Why do we use pointers in c++?
Why is null pointer used?
Can comments be nested?
Explain the static storage classes in c++.
What is general format for a prototype?
If you don’t declare a return value, what type of return value is assumed?
Where can I run c++ program?
How can a called function determine the number of arguments that have been passed to it?