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

Data Abstraction

Data abstraction allows programs to ignore details of how data is represented and focus on essential features. C++ classes use abstraction by defining attributes and functions to operate on them, putting essential object properties in a nutshell. Data members hold information, while member functions operate on them. Access labels like public and private enforce abstraction by controlling access to class members.

Uploaded by

Om K
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as DOCX, PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
138 views

Data Abstraction

Data abstraction allows programs to ignore details of how data is represented and focus on essential features. C++ classes use abstraction by defining attributes and functions to operate on them, putting essential object properties in a nutshell. Data members hold information, while member functions operate on them. Access labels like public and private enforce abstraction by controlling access to class members.

Uploaded by

Om K
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as DOCX, PDF, TXT or read online on Scribd
You are on page 1/ 4

What is Data Abstraction?

Data abstraction allows programs to ignore the details of how a data type is
represented. The word Abstraction (derived from the Latin word "abs", meaning away
from, and "trahere", meaning to draw) refers to the act of representing essential
features without including background details or explanations. C++ classes use
abstraction techniques and are defined as lists of abstract attributes, such as width,
cost, size, and functions, to operate on these attributes. They put in a nutshell all the
essential properties of an object that must be created. The attributes are therefore
called data members as they hold information. The functions that operate on those
data members are termed member functions. Since C++ classes use the data
abstraction concept, they are also termed abstract data types.

Data members and member functions are expressed in code when classifying a class.
But, when using an object (that is, an instance of a class), the built-in data type and
class members are ignored; This process is known as data abstraction. It is a
programming design technique that relies on separating interfaces from
implementations. So while designing your component, you being a programmer,
must keep the interface independent of the implementation because if you change
the underlying implementation, the interface will remain intact. C++ provides an
excellent level of data abstraction. In C++, we use classes to define our own abstract
data types (ADT). Programmers can use the "cout" object of class ostream for data
streaming to standard output like this:

Example:

#include <iostream>
using namespace std;

int main()
{
cout << "Hello World" << endl;
}

What is Abstract Class?


Abstract class in C++ is not to create objects. These classes are designed only to
treat like a base class (inherited by other classes). It is a developed technique for
program development that allows making a base upon which other classes can be
built.

Access Labels Enforce Abstraction


In C++ language, programmers can use access modifiers to define the abstract
interface of the class. A C++ class may contain zero or more access labels:

 As you all know, the members defined in Public Access Specifier are accessible
to certain program parts. Any data abstraction can be viewed or classified by
its public members.
 When the access specifier is in private mode, members defined in the private
mode are not accessible to code that uses the class. The private section
explicitly hides code implementation within a C++ program.
There is no limitation on how access modifiers may appear within a program. The
specific access modifier keeps its effect until the next access modifier is declared or
the closing brace (i.e. "}") of the class body is seen.

Program to Show the Use of Data Abstraction in C++

Here is an example of declaring Public members of a C++ class:

Example:

#include <iostream>
using namespace std;

class sample {
public:
int gl, g2;

public:
void val()
{
cout << "Enter Two values : "; cin >> gl >> g2;
}
void display()
{
cout << gl << " " << g2;
cout << endl;
}
};

int main()
{
sample S;
S.val();
S.display();
}
Output:

Enter Two values : 20


50
20 50
Here is a Private member example in which member data cannot be accessed outside
the class:

Example:

#include <iostream>
using namespace std;

class sample {
public:
int gl, g2;

public:
void val()
{
cout << "Enter Two values : "; cin >> gl >> g2;
}

private:
void display()
{
cout << gl << " " << g2;
cout << endl;
}
};

int main()
{
sample S;
S.val();
S.display();
}
If you execute the above program, the private member function will not be
accessible, and hence the following error message will appear like this in some
compilers:

Output:

error: 'void sample::display()' is private


void display()
^

Advantages of Data abstraction


 Class internals get protected from inadvertent user-level errors
 The programmer does not have to write the low-level code
 Code duplication is avoided, so the programmer does not have to go over
again and again fairly common tasks every time to perform a similar operation
 The main idea of abstraction is code reuse and proper partitioning across
classes.
 This may not seem helpful for small projects, but for large projects, it provides
conformity and structure as it provides documentation through the abstract
class contract.
 It allows internal implementation details to be changed without affecting the
users of the abstraction.

You might also like