0% found this document useful (0 votes)
26 views

Data Abstraction in C++: Access Labels Enforce Abstraction

Data abstraction refers to providing only essential information to the outside world and hiding implementation details. In C++, classes provide abstraction by defining public interfaces and private implementations. Abstraction separates interface and implementation so changing the underlying implementation does not impact programs using the interface.

Uploaded by

Sam
Copyright
© © All Rights Reserved
Available Formats
Download as PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
26 views

Data Abstraction in C++: Access Labels Enforce Abstraction

Data abstraction refers to providing only essential information to the outside world and hiding implementation details. In C++, classes provide abstraction by defining public interfaces and private implementations. Abstraction separates interface and implementation so changing the underlying implementation does not impact programs using the interface.

Uploaded by

Sam
Copyright
© © All Rights Reserved
Available Formats
Download as PDF, TXT or read online on Scribd
You are on page 1/ 2

DATA ABSTRACTION IN C++

http://www.tuto rialspo int.co m/cplusplus/cpp_data_abstractio n.htm Co pyrig ht © tuto rials po int.co m

Data abstraction refers to, providing only essential information to the outside world and hiding their backg round
details, i.e., to represent the needed information in prog ram without presenting the details.

Data abstraction is a prog ramming (and desig n) technique that relies on the separation of interface and
implementation.

Let's take one real life example of a T V, which you can turn on and off, chang e the channel, adjust the volume, and
add external components such as speakers, VCRs, and DVD players, BUT you do not know its internal details,
that is, you do not know how it receives sig nals over the air or throug h a cable, how it translates them, and finally
displays them on the screen.

T hus, we can say a television clearly separates its internal implementation from its external interface and you can
play with its interfaces like the power button, channel chang er, and volume control without having zero knowledg e
of its internals.

Now, if we talk in terms of C++ Prog ramming , C++ classes provides g reat level of data abstrac tion. T hey
provide sufficient public methods to the outside world to play with the functionality of the object and to manipulate
object data, i.e., state without actually knowing how class has been implemented internally.

For example, your prog ram can make a call to the sort() function without knowing what alg orithm the function
actually uses to sort the g iven values. In fact, the underlying implementation of the sorting functionality could
chang e between releases of the library, and as long as the interface stays the same, your function call will still
work.

In C++, we use c lasses to define our own abstract data types (ADT ). You can use the c out object of class
ostream to stream data to standard output like this:

#include <iostream>
using namespace std;

int main( )
{
cout << "Hello C++" <<endl;
return 0;
}

Here, you don't need to understand how c out displays the text on the user's screen. You need to only know the
public interface and the underlying implementation of cout is free to chang e.

Access Labels Enforce Abstraction:


In C++, we use access labels to define the abstract interface to the class. A class may contain zero or more
access labels:

Members defined with a public label are accessible to all parts of the prog ram. T he data-abstraction view
of a type is defined by its public members.

Members defined with a private label are not accessible to code that uses the class. T he private sections
hide the implementation from code that uses the type.

T here are no restrictions on how often an access label may appear. Each access label specifies the access level
of the succeeding member definitions. T he specified access level remains in effect until the next access label is
encountered or the closing rig ht brace of the class body is seen.

Benefits of Data Abstraction:


Data abstraction provides two important advantag es:

Class internals are protected from inadvertent user-level errors, which mig ht corrupt the state of the
object.
T he class implementation may evolve over time in response to chang ing requirements or bug reports
without requiring chang e in user-level code.

By defining data members only in the private section of the class, the class author is free to make chang es in the
data. If the implementation chang es, only the class code needs to be examined to see what affect the chang e may
have. If data are public, then any function that directly accesses the data members of the old representation mig ht
be broken.

Data Abstraction Example:


Any C++ prog ram 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 the following result:

Total 60

Above class adds numbers tog ether, and returns the sum. T he public members addNum and g etT otal are the
interfaces to the outside world and a user needs to know them to use the class. T he private member total is
something that the user doesn't need to know about, but is needed for the class to operate properly.

Desig ning Strateg y:


Abstraction separates code into interface and implementation. So while desig ning your component, you must
keep interface independent of the implementation so that if you chang e underlying implementation then interface
would remain intact.

In this case whatever prog rams are using these interfaces, they would not be impacted and would just need a
recompilation with the latest implementation.

You might also like