Data abstraction in C ++

Data abstraction involves only providing the necessary information to the outside and hiding their basic details, for example, to represent the information needed in the program without displaying the details. Details about them.

Data abstraction involves only providing the necessary information to the outside and hiding their basic details, for example, to represent the information needed in the program without displaying the details. Details about them.

Data abstraction is a programming technique that is based on the distinction of Interface and Implementation.

Considering an example of a TV, you can turn on / off, change channels, adjust volume, and add peripherals such as speakers, VCRs and DVDs. But you don't know the details of its contents, that is, you don't know how it receives signals over the air or through cables, how to translate them and finally display them on the screen.

Therefore, it can be said that a TV that clearly distinguishes its internal deployment with peripheral interface and you can manipulate the interface with power buttons and volume controls without any understanding. know about what's going on inside it.

Now, in terms of C ++ programming language, C ++ classes provide Data abstraction at a great level. They provide enough public to external methods to manipulate the feature of the object and to manipulate object data, for example: the state without actually knowing how the class was implemented internally. .

For example, your program may make a call to the sort () function without knowing what function it actually uses to sort the given values. In fact, the underlying implementation of the sorting feature may vary depending on the library, and as long as Interface remains the same, your function call will continue to work.

In C ++, we use Classes to define our own abstract data types (ADT) (ADT). You can use the cout object of the ostream class for the data stream to the standard output as follows:

 #include using namespace std ; int main ( ) { cout << "Hello C++" << endl ; return 0 ; } 

Here, you don't need to understand how cout displays text on the screen. You only need to know Public Interface and Underlying Implementation of cout is ready to change.

Access Label in C ++

In C ++, we use Access Label to define Abstract Interface for the class. A class can contain 0 or more Access Labels.

The members defined with a public label are accessible to all parts of the program. Abstract the data of a type defined by its public members.

Members defined with a private label are not accessible for code that uses that class. The private area hides this deployment with code that uses that type.

There is no restriction on the frequency with which an access label may appear. Each Access Label determines the access level of the next member definition. The specified access level remains effective until the next Access Label is encountered or the close parentheses of the class body are encountered.

Benefits of Data Abstraction in C ++

Data abstraction in C ++ offers two important advantages:

The internal or internal part of the layer is protected from unintentional user errors, which can damage the state of the data.

Class deployments can be carried out over time to meet report changes or bug requests without requiring changes in user code.

By defining data members only in the private area of ​​the class, the author of the class can freely make changes in the data. If the implementer changes, then only layer encryption is needed to check which aspect brings change. If the data is public, then any function that accesses directly to the data members of the old representation can be broken.

Example of data abstraction in C ++

In any C ++ program, where you deploy a class with members that are public and private, it is an example of data abstraction. Consider the following example:

 #include using namespace std ; class A { public : // khai bao constructor A ( int i = 0 ) { tong = i ; } // du lieu ma la nhin thay voi ben ngoai void congThem ( int motso ) { tong += motso ; } // du lieu ma la nhin thay voi ben ngoai int tinhTong () { return tong ; }; private : // du lieu ma la bi an voi ben ngoai int tong ; }; int main ( ) { A a ; a . congThem ( 15 ); a . congThem ( 25 ); a . congThem ( 35 ); cout << "Tong gia tri la: " << a . tinhTong () << endl ; return 0 ; } 

Compiling and running the above C ++ program will produce the following results:

 Tong gia tri la : 75 

The upper class adds two numbers and returns their sum. Public members are congThem and tinhTong are Interface (which are visible) to the outside and a user needs to know them to use that class. The member private is tong which is something the user doesn't need to know, but is necessary for the class to function correctly.

Design strategy in C ++

Data abstraction distinguishes code into Interface and Implementation. So while designing your component, you have to keep Interface independent of Implementation, so that if you change the underlying implementation, the Interface will still exist.

In this case, any program is using these Interface, they will not be affected and will need a re-compile with this latest Implementation.

According to Tutorialspoint

Previous article: Polymorphism in C ++

Next lesson: Calculating closure in C ++

4 ★ | 1 Vote | 👨 187 Views
« PREV POST
NEXT POST »