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
How do you print a string on the printer?
What is the use of 'this' pointer?
How can I disable the "echo" feature?
Is dev c++ a good compiler?
Write a program which is required to process the time of a clock in hours and minutes, entered from the keyboard. With this program, there are two requirements for any data entered by a user: 1. The data must be of the correct type (in this case, two ints). 2. The data must be in the correct range: this means that, for the minutes, negative numbers and any number above 59 must be rejected; for the hours, negative numbers and any number above 23 must be rejected. Output error message for invalid data input. Output the time one and a half hour after the time input. i.e. Hour: 22 Min: 32 One and a half hour after 22:32 is 00:02
What is a sequence in c++?
Is c++ free?
What is doubly linked list in c++?
Program to check whether a word is a sub-string or not of a string typed
What are special characters c++?
Write about the members that a derived class can add?
what are the types of Member Functions?
What is the difference between cin.read() and cin.getline()?
What do you mean by stack unwinding in c++?
Evaulate: 22%5 a) 2 b) 4 c) 0