Abstraction in C++

Advertisements

Inheritance in C++ Encapsulation in C++

Real Life Example of Abstraction in C++

Abstraction is the concept of exposing only the required essential characteristics and behavior with respect to a context.

Hiding of data is known as data abstraction. In object oriented programming language this is implemented automatically while writing the code in the form of class and object.

Abstraction is one of the feature of Object Oriented Programming, where you show only relevant details to the user and hide irrelevant details. You can understand with this 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. Real life example of Abstraction in C++

Abstraction shows only important things to the user and hides the internal details for example when we ride a bick, we only know about how to ride bick but can not know about how it work ? and also we do not know internal functionality of bick.

real life example of abstraction in c++

2. Real life example of Abstraction in C++

Consider a real life example of a man driving a car. The man only knows that pressing the accelerators will increase the speed of car or applying brakes will stop the car but he does not know about how on pressing accelerator the speed is actually increasing, he does not know about the inner mechanism of the car or the implementation of accelerator, brakes etc in the car.

3. Real life example of Abstraction in C++

Suppose you are operating a mobile phone. You know that the important feature of a phone is the memory card, SIM, battery life, design and build, and processor power. But, while operating the phone you do not get into the operational details of the phone such as how the CPU of the phone allocates memory for the various media on your phone or other intricate architectural details of the phone. All these details aren’t visible to cell-phone users from a non-technical background.

real life example of abstraction in c++

Example of Abstraction in C++

#include<iostream.h>
#include<conio.h>

class sum
{
// hidden data from outside world
private: int a,b,c;

public:
void add()
{
clrscr();
cout<<"Enter any two numbers: ";
cin>>a>>b;
c=a+b;
cout<<"Sum: "<<c;
}
};
void main()
{
sum s;
s.add();
getch();
}

Output

Enter any two number: 
4
5
Sum: 9

Note: Data abstraction can be used to provide security for the data from the unauthorized methods.

Note: Note: In C++ language data abstraction can be achieve by using class.

Difference Between Abstraction and Encapsulation

Abstraction deals with hiding the details and showing the essential things to the user whereas encapsulation bind your data and code together as a single unit.

Encapsulation is not providing full security because we can access private member of the class using reflection API, but in case of Abstraction we can't access static, abstract data member of a class.

Advantages of Data Abstraction in C++

Here we discuss some of its benefits in order to acknowledge its significance. The benefits of data abstraction are given below;

  • Data abstraction increases the reusability of the code by avoiding any chances of redundancy.
  • It increases the readability of the code as it eliminates the possibility of displaying the complex working of the code.
  • With the implementation of classes and objects, comes enhanced security. Since data abstraction is a method of implementing classes and objects any denying access to other classes of accessing the data members and member functions of the base class.
  • It helps the user to write a high-level code.
  • It separates the entire program into code and implementation making it more comprehensible.
  • 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.

Implementaion of Abstraction

Below we discuss about some way to Implement Abstraction.

Abstraction using Classes: We can implement Abstraction in C++ using classes. Class helps us to group data members and member functions using available access specifiers. A Class can decide which data member will be visible to outside world and which is not.

Abstraction in Header files: One more type of abstraction in C++ can be header files. For example, consider the pow() method present in math.h header file. Whenever we need to calculate power of a number, we simply call the function pow() present in the math.h header file and pass the numbers as arguments without knowing the underlying algorithm according to which the function is actually calculating power of numbers.

Abstraction using access specifiers: Access specifiers are the main pillar of implementing abstraction in C++. We can use access specifiers to enforce restrictions on class members.
For example;

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

Designing Strategy of Abstraction

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.


Inheritance in C++ Encapsulation in C++

Google Advertisment

Buy This Ad Space @$20 per Month, Ad Size 600X200 Contact on: hitesh.xc@gmail.com or 9999595223

Magenet is best Adsense Alternative here we earn $2 for single link, Here we get links ads. Magenet

For Projects 9999595223

Google Advertisements


Buy Websites 9999595223

Buy College Projects with Documentation Contact on whatsapp 9999595223. Contact on: hitesh.xc@gmail.com or 9999595223 Try this Keyword C++ Programs

Advertisements