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
- 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
May be interested
- The way to teach children fast mental arithmetic of Japanese peopledare to say that you will never find such interesting mental math or math rules in textbooks!
- 3 ways to calculate totals in Excelto calculate totals on excel, users can use them in three different ways, based on spreadsheets, autosum functions or sum functions with large data areas.
- How to calculate NPV in Excelhow to calculate npv in excel. npv is the net present value (npv), that is, the net present value of an investment. based on the npv, the investor calculates the difference between the current value of cash inflows and the current value of cash outflows.
- How to calculate the distance to view TV reasonably and safely for healthchoosing the right distance when watching television not only helps you fully enjoy the appeal of the entertainment program, but more importantly ensures health safety, especially eyes.
- Instructions for calculating inches for TVshow to calculate the standard size to choose for yourself the tv that best suits your needs.
- How to calculate the number of days in Excelhow many days from january 9, 2015 to june 12, 2017? if you can find results within 5 seconds, you are truly a genius. however, this is no longer a problem when you know how to use excel.
- How to calculate right in every application on iPhonenow users can perform calculations right in any app without having to exit and then access the calculator app on ios 18.
- Calculation of percentages in Excelcalculating percentages in excel is a basic calculation, with a very simple and quick calculation when you don't need to enter any functions.
- How to Calculate Data Transfer Speeddata transfer rate (dtr) is the rate at which a certain amount of data is transferred in a specific period of time. you'll probably want to know what this speed is if you're downloading something from the internet or transferring data from one source to another. first, change the units so that the file size and transfer rate are both bits or bytes and have the same prefix (kilo, mega, giga, or tera). then plug your numbers into the formula s = a ÷ t, where a is the amount of data and t is the transmission time to calculate s (transfer rate). you can also calculate the amount of data or transfer time if you know either of these variables and the transfer rate.
- SUM function in Excel: How to use SUM to calculate totals in Excel - SUM function in Excelthe sum function in excel is quite commonly used. below are details on how to calculate the sum function and related issues in microsoft excel.