2. Data Abstraction in C++ …
Data Abstraction is a process of providing only the essential details to
the outside world and hiding the internal details, i.e., representing only
the essential details in the program.
Data Abstraction is a programming technique that depends on the
separation of the interface and implementation details of the program.
C++ provides a great level of Abstraction.
For example, pow() function is used to calculate the power of a number
without knowing the algorithm the function follows.
3. Consider one real life example of a TV, which you can turn on and off, change 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
signals over the air or through a cable, how it translates them, and finally displays them on
the screen.
Thus, 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 changer, and
volume control without having any knowledge of its internals.
C++ provides great level of data abstraction. It provides 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.
4. In C++, we use classes to define our own abstract data types (ADT). You can use the cout object
of class ostream to stream data to standard output like this −
#include< iostream> output :-
using namespace std ; Hello C++
int main( )
{
cout << “ Hello C++ “ << endl ;
Return 0 ;
}
Here, you don't need to understand how cout 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 change.
5. C++ classes use the technique of abstraction and are defined
as a list of abstract attributes and functions to operates on
them.
The Attributes which holds all the information about the
properties of the object required to form are called data
members.
The functions which operates on Data Members are called
Member Functions.
Access specifiers are used to Implement Abstraction.
6. Access Specifiers in C++
Access specifiers define how the members (attributes and methods) of a
class can be accessed.
Access Specifiers Implement Abstraction:
• Public specifier: When the members are declared as public members can be
accessed anywhere from the program.
• Protected Specifier : members cannot be accessed from outside the class,
however, they can be accessed in inherited classes.
• Private specifier: When the members are declared as private members can
only be accessed only by the member functions of the class.
7. Data Abstraction can be achieved in two ways:
• Abstraction using classes
• Abstraction in header files.
8. Abstraction using Classes
#include<iostream>
using namespace std; Output :-
class Sum {
Enter two numbers :
private : int x , y, x; 3
public : 6
void add( ) { Sum of two numbers is : 9
cout << “ Enter two numbers : “ ;
cin >> x >> y ;
z= x + y ;
cout<<“ Sum of two numbers is : “ << z << endl ;
}
} ;
int main ()
{
Sum sm;
sm.add();
return 0 ;
}
9. Abstraction in Header Files
// Program to print power of a number :
#include<iostream.h> Output :-
#include<math.h>
Using namespace std ; The Value is : 81
int main( ) {
int x = 3 ;
int power = 4;
int result = pow( x, power) ;
cout <<“ The Value is : “ << result << endl ;
return 0 ;
}
10. Types of Abstraction..
There are 2 types of abstraction :-
1. Data Abstraction
Hiding the details about the data is called data abstraction.
2. Control Abstraction
Hiding the details about the implementation is called control abstraction.
11. Advantages of Abstraction..
Increases reusability of the code.
Avoids duplication of your code.
Makes the application secure by not allowing anyone else to see the
background details.
A programmer does not need to write the low level code.
Internal implementation can be changed without affecting the user
level code.
12. Designing Strategy
Abstraction separates code into interface and implementation.
So while designing your component, you must keep interface independent of the
implementation so that if you change underlying implementation then interface
would remain intact.
In this case whatever programs are using these interfaces, they would not be
impacted and would just need a recompilation with the latest implementation.