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


Please Help Members By Posting Answers For Below Questions

Evaulate: 22%5 a) 2 b) 4 c) 0

672


Define a pointer to a data member of the type pointer to pointer?

579


Why c++ is faster than java?

601


Mention the purpose of istream class?

625


What is class and structure in c++?

563






What is a stack c++?

578


Is swift better than c++?

542


Why do we use pointers in c++?

596


Why is null pointer used?

686


Can comments be nested?

632


Explain the static storage classes in c++.

709


What is general format for a prototype?

601


If you don’t declare a return value, what type of return value is assumed?

542


Where can I run c++ program?

605


How can a called function determine the number of arguments that have been passed to it?

662