0% found this document useful (0 votes)
9 views3 pages

Data Abstracton

Uploaded by

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

Data Abstracton

Uploaded by

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

Data Abstraction in C++

Data Abstraction is a process of providing only the essential details to the


outside world and hiding the internal details

Data Abstraction can be achieved in two ways:

o Abstraction using classes


o Abstraction in header files.

Access Specifiers Implement Abstraction:

o Public specifier: When the members are declared as public, members can
be accessed anywhere from the program.
o Private specifier: When the members are declared as private, members can
only be accessed only by the member functions of the class.

#include <iostream.h>
#include<math.h>
int main()
{
int n = 4;
int power = 3;
int result = pow(n,power);
cout << "Cube of n is : " <<result<<endl;
return 0;
}
Output:

Cube of n is : 64

Simple example of data abstraction using classes.

#include <iostream>
using namespace std;
class Sum
{
private: int x, y, z; // private variables
public:
void add()
{
cout<<"Enter two numbers: ";
cin>>x>>y;
z= x+y;
cout<<"Sum of two number is: "<<z<<endl;
}
};
int main()
{
Sum sm;
sm.add();
return 0;
}

Output:

Enter two numbers:


3
6
Sum of two number is: 9
In the above example, abstraction is achieved using classes. A class 'Sum' contains
the private members x, y and z are only accessible by the member functions
of the class.

You might also like