Calculate closure in C ++
All C ++ programs include two basic components:
- Program commands : This is the part of the program that performs the actions and offerings called functions.
- 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)
You should read it
- How to use Closure in Python
- Intel released the computer to remove the Compute Stick bag
- Data abstraction in C ++
- Microsoft officially set the death date for Wunderlis application
- Google implemented a virtual server solution for Cloud Compute Engine platform
- Samsung, Xiaomi, Oppo and LG all simultaneously closed production plants in India
- Options to create data queries in Access 2016
- Summary of the common Run CMD commands
- How to Exit out of a Frozen Computer Program
- Evan Blass suddenly announced the closure of @evleaks
- China's three largest Bitcoin trading points are about to close
- Free online visit to the Louvre
Maybe you are interested
How to get data from web into Excel
What information does a VPN hide? How does it protect your data?
How to transfer data between 2 Google Drive accounts
6 Data Collecting Apps You Need to Delete for Better Privacy
How to master numerical data in Google Sheets with the AVERAGE function
How to delete white space in a table in Word - Appears right below the data