C++ Abstraction

Download as pdf or txt
Download as pdf or txt
You are on page 1of 7

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.

Data Abstraction is a programming technique that depends on the separation of the


interface and implementation details of the program.

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).

Types of Abstraction in C++

There are 2 types of abstraction in C++:

1. Data Abstraction: It hides the information about the data.


2. Control Abstraction: It hides the information about the implementation.

Ways to achieve Abstraction


Data Abstraction can be achieved in two ways:

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.

Abstraction using Access modifiers


Access modifiers are the main pillar of implementing abstraction in C++. We can use
access specifiers to enforce restrictions on class members.

• Members declared as public in a class, can be accessed from anywhere in the


program.
• Members declared as private in a class, can be accessed only from within the class.
They are not allowed to be accessed from any part of code outside the class.
Example 1

#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

…Program finished with exit code 0


Press ENTER to exit console.

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

Sum of the two numbers = 30

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;

// pow(n, power) is the power function


int result = pow(n, power);

cout << "Cube of n = " << result;


return 0;
}
Output

Cube of n = 8

Example 3
#include <iostream>
#include <string>
using namespace std;
class employee{
int empId;
string name;
double salary,basic,allowances;

double calculateSalary(int empId){


salary = basic+allowances;
return salary;
}
public:
employee(int empId, string name,double basic,double allowances):
empId(empId),name(name),basic(basic),allowances(allowances){
calculateSalary(empId);
}

void display(){

cout<<"EmpId = "<<empId<<"\tName = "<<name<<endl;


cout<<"Employee Salary = "<<salary;

}
};
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.

8. The programmer need not write low-level code.

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.

You might also like