C++ Data Abstraction
Object oriented programming offers various features to write programs with various concepts that help to minimize problems and increase flexibility in program. One of the features of object oriented programming is Data abstraction. In this chapter you will learn about how the concept data abstraction is carried out within the C++ program.
What is Data abstraction?
Data abstraction allows a program to ignore the details of how a data type is represented. Abstraction (derived from a Latin word abs, meaning away from and trahere, meaning to draw) refers to the act of representing essential features without including the background details or explanations. C++ classes use the technique of abstraction and are defined as a list of abstract attributes such as width, cost, size etc and functions to operate on these attributes. They put in a nutshell all the essential properties of an object that are required to be created. The attributes are therefore called data members as they hold information. The functions that operate on those data members are termed as member functions. Since C++ classes use the data abstraction concept, they are also termed as abstract data types.
While classifying a class, both data members and member functions are expressed in the code. But, while using an object (that is an instance of a class) the built in data types and the members in the class get ignored which is known as data abstraction. It is a programming design technique that depends on separation of interface to that of implementation. So while designing your component, you being a programmer must keep interface independent of the implementation because if you change the underlying implementation then interface would remain intact. C++ provides a great 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 the one which is not used to create objects. These type of classes are designed only to treat like a base class (to be inherited by other classes). It is a designed technique in program development which allows to make a base upon which other classes may 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:
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 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();
}
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;
}
void display()
{
cout << gl << " " << g2;
cout << endl;
}
};
int main()
{
sample S;
S.val();
S.display();
}
If we execute the above code, the following error message will appear like this:
Comments
Post a Comment