C++ - SPLessons
SPLessons 5 Steps, 3 Clicks
5 Steps - 3 Clicks

Abstraction in C++

Abstraction in C++

shape Description

Abstraction in CPP can be defined as one of the pillar of OOPs, which shows only required features for the user and hides the operation performed to make that feature. If an example of Calculator is taken in real time, one can use it different ways by giving various values and perform operations. But, mechanism how the operation is done is not shown. This process of hiding the information is called as Abstraction. In C++, the compiler gives access to only few methods of classes in order to provide security to the application. The compiler does not show how the remaining methods work as they are hided. For example, input is taken using cin. One just need to give the information to the compiler following the syntax of the language. There is no need to know how cin takes the input and connect to various methods.

Abstract Data type

shape Description

Classes use the concept of Abstraction and are defined as a list of abstracted data and functions to operate on these data. Classes encapsulates all the essential properties of the object that are to be created. Since the classes use the concept of Data Abstraction in CPP , they are know as Abstracted Data types and are user-defined data types. It is similar to data structures but maintains the modularity of the program. One need to define the data and operations to be performed on the data type. This can be defined using construct class which consists of instances. 

shape More Info

Access modifiers in C++ helps in defining the interface of abstraction to the class. As seen in the previous lessons, public and private are the access modifiers giving the scope of the methods. No restrictions are maintained on how to use and when to use these access modifiers.
  • public members are viewed and accessed by the all the other members. Data Abstraction is performed by using accessing only required public members.
  • private members are not viewed and accessed by any other other members of the class. Data Abstraction implements its hiding property using private members. By default, class members are private.

shape Example

[c]#include <iostream> using namespace std; class Add { public: // defining constructor Add(int x = 0) { sum = x; } // interface to outside world void addNumber(int num) { sum = sum + num; } // interface to outside world int getSum() { return sum; }; private: int sum; //hidden data }; int main( ) { Add ad; ad.addNumber(5); ad.addNumber(10); ad.addNumber(20); cout << " Sum of given values is " << ad.getSum() <<endl; return 0; }[/c] Output [c] Sum of given values is 35[/c]

Summary

shape Key Points

  • Abstraction in CPP is the process of showing only the essential features by hiding the implementation details.
  • The user defined types can be given using abstraction concept called as Abstract Types.
  • Access modifiers defines the scope.

shape Programming Tips

Abstraction in CPP must be such that the code is split into two categories. One is the interface and the other is the interface implementation. One category should not dependent on other category. So, no problem in implementation occurs even if changes are made to the interface.