1. Object Oriented Programming in C++
Central University Of Kashmir
Data Abstraction in
C++
Presented by : Peerzada Sameem Makhdoomi
Enrollment no. 2124CUKmr09
2. • Data Abstraction is a programming technique that depends on the separation of
the interface and implementation details of the program.
• 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.
• let's take a real life example of AC, which can be turned ON or OFF, change the
temperature, change the mode, and other external components such as fan,
swing. But, we don't know the internal details of the AC, i.e., how it works
internally. Thus, we can say that AC separates the implementation details from
the external interface.
• 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.
What is data abstraction ?
3. Data Abstraction can be achievedin two ways:
Abstraction using classes
Abstraction in header files.
Abstraction using classes:- An abstraction can be achieved using
classes. A class is used to group all the data members and member functions
into a single unit by using the access specifiers. A class has the responsibility
to determine which data member is to be visible outside and which is not.
Abstraction in header files:- An another type of abstraction is header
file. For example, pow() function available is used to calculate the power of
a number without actually knowing which algorithm function uses to
calculate the power. Thus, we can say that header files hides all the
implementation details from the user.
4. Access Specifiers Implement Abstraction:
In C++, we use access labels to define the abstract interface to the class.
A class may contain zero or more access labels
o Members defined with a public label are accessible to all parts of the program.
The data-abstraction view of a type is defined by its public members.
o Members defined with a private label are not accessible to code that uses the
class. The private sections hide the implementation from code that uses the type
5. Advantages of Data Abstraction:
Helps the user to avoid writing the low level code
Avoids code duplication and increases reusability.
Can change internal implementation of class independently without affecting
the user.
Helps to increase security of an application or program as only important
details are provided to the user.
6. i. 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.
ii. In this case whatever programs are using these interfaces, they
would not be impacted and would just need a recompilation with
the latest implementation.
Designing Strategy:
7. Let's see a simple example of abstraction in header files.
// Program to calculate the power of any number.
#include <iostream>
#include<math.h>
using namespace std;
int main( ) {
int number , result , power = 3;
cout<<“Enter any number:”<<endl; // for example 4
cin>>number;
result = pow (number , power); // pow (number , power) is the power function
cout << "Cube of the number is : "<<result<< endl;
return 0;
}
Output:
Cube of the number is : //64
In the above example, pow() function is used to calculate 4 raised to the power 3. The pow()
function is present in the math.h header file in which all the implementation details of
the pow() function is hidden.
8. #include <iostream>
using namespace std;
class implementAbstraction{
private:
int a, b;
public:
// method to set values of
// private members
void set(int x, int y)
{ a = x;
b = y; }
void display() {
cout<<"a = " <<a << endl;
cout<<"b = " << b << endl;
}
};
int main()
{
implementAbstraction obj;
obj.set(10, 20);
obj.display();
return 0;
}
example:
9. Output:
a = 10 b = 20
You can see in the above program we are not allowed
to access the variables a and b directly, however one
can call the function set() to set the values in a and b
and the function display() to display the values of a
and b.