C++ data encapsulation
All C++ programs have the following two basic elements:
Program statements (code): This is the part of the program that performs actions , they are called functions.
Program data: Data is program information and will be affected by program functions.
Encapsulation is a concept in object-oriented programming that binds data and functions that operate on the data together. This can avoid interference and misuse from the outside world, thereby ensuring security. Data encapsulation leads to another important OOP concept, namely data hiding.
Data encapsulation is a mechanism that bundles data and functions that operate on data. Data abstraction is a mechanism that only exposes the interface to the user and puts the specific A mechanism whereby implementation details are hidden.
C++ supports encapsulation and data hiding (public, protected, private) by creating classes. We already know that a class contains private, protected and public members. By default, all items defined in a class are private. For example:
class Box { public: double getVolume(void) { return length * breadth * height; } private: double length; // 长度 double breadth; // 宽度 double height; // 高度 };
The variables length, breadth and height are all private. This means that they can only be accessed by other members of the Box class and not by other parts of the program. This is one way to achieve encapsulation.
In order to make the members of a class public (that is, accessible to other parts of the program), they must be declared using the public keyword before these members. All variables or functions defined after a public identifier can be accessed by all other functions in the program.
Defining a class as a friend class of another class will expose implementation details, thus reducing encapsulation. The ideal approach is to hide the implementation details of each class from the outside as much as possible.
Instances of data encapsulation
In a C++ program, any class with public and private members can be used as an instance of data encapsulation and data abstraction. See the following example:
#include <iostream> using namespace std; class Adder{ public: // 构造函数 Adder(int i = 0) { total = i; } // 对外的接口 void addNum(int number) { total += number; } // 对外的接口 int getTotal() { return total; }; private: // 对外隐藏的数据 int total; }; int main( ) { Adder a; a.addNum(10); a.addNum(20); a.addNum(30); cout << "Total " << a.getTotal() <<endl; return 0; }
When the above code is compiled and executed, it produces the following results:
Total 60
The above class adds numbers and returns the sum. Public members addNum and getTotal are external interfaces, and users need to know them in order to use the class. The private member total is hidden from the outside and users do not need to know about it, but it is necessary for the class to work properly.
Design strategy
Normally, we will set the class member status to private unless we really need to expose it, so as to ensure good encapsulation.
This usually applies to data members, but it applies equally to all members, including virtual functions.