C++ Abstraction
C++ Abstraction
C++ Abstraction
Data abstraction is one of the most essential and important features of object-oriented
programming in C++. Abstraction means displaying only essential information and hiding
the details.
For Example: - when you send an email to someone you just click send and you get the
success message, what actually happens when you click send, how data is transmitted
over network to the recipient is hidden from you (because it is irrelevant to you).
1. Using classes
The simplest way to achieve or implement abstraction is to use classes. A class can be used to
group every data member and function into a solo unit with help of access specifiers. A class
can determine which data of the class could be accessible to the outside world. In this way with
the use of access specifiers, classes help to implement abstraction.
#include <iostream>
using namespace std;
class abstraction {
public:
int a, b;
public:
// method to set value of private members
void value(int x, int y) {
a = x;
b = y;
}
void display() {
cout << "The value of a = " << a << endl;
cout << "The value of b = " << b << endl;
}
};
int main() {
abstraction obj;
obj.value(2, 4);
obj.display();
return 0;
}
Output
The value of a = 2
The value of b = 4
Example 2
#include <iostream>
using namespace std;
class dataAbstraction {
public:
int a, b;
public:
void data()
{
cout << "Welcome to TechVidvan! \n";
cout << "Enter Two values : ";
cin >> a >> b;
}
public:
void display()
{
cout << "The sum is "<< a+b;
cout << "\n";
}
};
int main()
{
dataAbstraction dA;
dA.data();
dA.display();
}
Output:
Welcome to TechVidvan!
Enter Two values : 4 9
The sum is 13
Example 3
#include <iostream>
#include <string>
using namespace std;
class sample
{
int num1,num2;
void readNum(){
cout<<"Enter num1 : "; cin>>num1;
cout<<"\nEnter num2 : "; cin>>num2;
}
public:
void displaySum()
{
readNum();
cout<<"\nSum of the two numbers = "<<num1+num2<<endl;
}
};
int main()
{
sample s;
s.displaySum();
}
Output:
Enter num1 : 10
Enter num2 : 20
Example 4
#include <iostream>
using namespace std;
class dataAbstraction {
public:
int a, b;
public:
void data()
{
cout << "Welcome to TechVidvan! \n";
cout << "Enter Two values : ";
cin >> a >> b;
}
private:
void display()
{
cout << "The sum is "<< a+b;
cout << "\n";
}
};
int main()
{
dataAbstraction dA;
dA.data();
dA.display();
}
2. Through header files
Another type of abstraction in C++ is with header files. For example, if we put the header file for
mathematical operations i.e. <cmath> or <math.h>, at the start of the program we can use the
sqrt() function to find the square root of any given number. In this way, we know what sqrt()
does, but we do not know the background details of how it finds the square root.
Example 1
#include <iostream>
#include <cmath>
using namespace std;
int main()
{
double num, answer;
cout<<"Enter a number: ";
cin>>num;
answer = sqrt(num);
cout << "\n Square root of " << num << " is " << answer ;
}
Output:
Enter a number: 49
Square root of 49 is 7
…Program finished with exit code 0
Press ENTER to exit console.
Example 2
#include <iostream>
#include <math.h>
using namespace std;
int main() {
int n = 2;
int power = 3;
Cube of n = 8
Example 3
#include <iostream>
#include <string>
using namespace std;
class employee{
int empId;
string name;
double salary,basic,allowances;
void display(){
}
};
int main()
{
employee emp(1,"Ved",15000,3245.43);
emp.display();
}
Output:
EmpId = 1 Name = Ved
Employee Salary = 18245.4
Benefits of Abstraction in C++
1. Abstraction increases the reusability of the code because of the proper partitioning.
2. It reduces the complexity as well as the redundancy of the code, hence increasing the
readability.
3. Using classes and objects increases the security of the code. We can declare the parts of the
code as private to keep them secure.
4. Due to abstraction, the important parts of the code are secure as only the essential features
are provided to the user and they don’t know the background details.
5. Since the programmer can use the same code repeatedly, it helps us perform similar tasks for
similar operations.
6. Abstraction allows changing internal implementations without affecting its user-level code.
7. Only the programmer can make changes to his data and methods, no one else.
Advantages Of Abstraction
• The programmer need not write low-level code.
• Abstraction protects the internal implementation from malicious use and errors.
• Abstraction can prevent code duplication and thus the programmer needs to perform
the same tasks over and over again.
• Abstraction promotes code reuse and properly classifies the class data members.
• The Programmer can change the internal details of the class implementation without
the knowledge of end-user thereby without affecting the outer layer operations.