Calculate closure in C ++

Encapsulation is a concept of object-oriented programming that binds data and functions that manipulate that data, and keeps them safe by preventing obstruction and external abuse. . Closedness leads to the important OOP concept Data Hiding.

All C ++ programs include two basic components:

  1. Program commands : This is the part of the program that performs the actions and offerings called functions.
  2. Program data: Data is information of the program that affects program functions.

Encapsulation is a concept of object-oriented programming that binds data and functions that manipulate that data, and keeps them safe by preventing obstruction and external abuse. . Closedness leads to the important OOP concept Data Hiding .

Data encapsulation is a technique that encapsulates data, and functions that use them and data abstraction is a technique that only displays the Implementation Detail and Interface (deployment details) to user.

C ++ supports the properties of packaging and hiding data through the creation of self-defined types (user-defined), called classes. We learned that a class can contain private, protected and public members . By default, all components defined in a class are private. For example:

 class Box { public : double tinhTheTich ( void ) { return chieudai * chieurong * chieucao ; } private : double chieudai ; // Chieu dai cua mot box double chieurong ; // Chieu rong cua mot box double chieucao ; // Chieu cao cua mot box }; 

The variables chieudai, chieurong, and chieucao are private . Meaning they can only be accessed by other members of the Box class, and not by any other part of your program. This is a way to implement encapsulation in C ++.

To make the parts of the class public (for example, being able to access other parts of your program), you must declare them after the public keyword. All variables and functions defined after the public keyword are accessible for all functions in your program.

Example of closure in C ++

In any C ++ program, where you deploy a class with members is public and private, that is an example of closure and data abstraction in C ++. 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 ++

Most of us have had little experience of making class members private by default, unless we really need to display them. It was a good packaging experience.

This experience is most often applied to data members, but it applies equally to all members, including virtual functions in C ++.

According to Tutorialspoint

Previous article: Abstracting data in C ++

Next lesson: Interface in C ++ (Abstract class)

5 ★ | 1 Vote | 👨 226 Views
« PREV POST
NEXT POST »