Coding With Fun
Home Docker Django Node.js Articles Python pip guide FAQ Policy

The data encapsulation for C+


May 11, 2021 C++


Table of contents


The data encapsulation for C+

All of the programs have two basic elements:

  • Program statement (code): This is the part of the program that performs the action, and they are called functions.
  • Program data: Data is the information of a program and is affected by program functions.

Encapsulation is a concept in object-oriented programming that binds functions of data and operational data together to avoid outside interference and misuse, thus ensuring security. The data encapsulation leads to another important OOP concept, data hiding.

Data encapsulation is a mechanism that binds data to functions of operational data, and data abstraction is a mechanism that exposes interfaces only to the user and hides specific implementation details.

C++ supports encapsulation and data by creating classes. A s we already know, classes contain private members, protection members, and public members. B y default, all items defined in the 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 private. T his means that they can only be accessed by other members of the Box class, not by other parts of the program. This is one way to implement encapsulation.

In order for members of a class to become public (that is, other parts of the program are also accessible), they must be declared using the public keyword before those members. All variables or functions defined behind the public identifier can be accessed by all other functions in the program.

Defining one class as a friend class of another exposes implementation details, reducing encapsulation. Ideally, the implementation details of each class should be hidden from the outside as much as possible.

An instance of a data encapsulation

Any class with public and private members can be used as an example of data encapsulation and data abstraction in a C++ program. Take a look at 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 class above adds the numbers and returns the sum. P ublic members addNum and getTotal are external interfaces that users need to know in order to use classes. Private member total is hidden from the outside, and the user does not need to know about it, but it is necessary for the class to work properly.

Design the strategy

Typically, we set the class member state to private unless we really need to expose it to ensure good encapsulation.

This usually applies to data members, but it also applies to all members, including virtual functions.