Latest web development tutorials

C ++ data encapsulation

All C ++ programs have two basic elements:

  • Program statements (code): This is part of a program to perform an action, they are called functions.
  • Program Data: Data is program information, program functions will be affected.

Encapsulation is a concept of object-oriented programming in the function data and manipulating data bound together, so to avoid outside interference and misuse, thereby ensuring safety. Data encapsulation come out of another important OOP concepts, namely,data hiding.

Data encapsulation is a mechanism to function data and manipulating data bundled data abstractionis only one exposed to the user interfaces and the implementation details hidden mechanism.

By creating a C ++class support encapsulation and data hiding (public, protected, private).We already know that the class contains private members (private), to protect members of the (protected) and members of the public (public) members. By default, all items defined in the class are private. E.g:

class Box
{
   public:
      double getVolume(void)
      {
         return length * breadth * height;
      }
   private:
      double length;      // 长度
      double breadth;     // 宽度
      double height;      // 高度
};

Variable length, breadth and height are private (private). This means that they can only be accessed by other members of the Box class, you can not access other parts of the program. This is a way to achieve package.

In order to become a member of the public class (that is, other parts of the program can access), you must use thepublic keyword before these members are declared.All identifiers are defined in the public behind the variable or function can be used by all other functions to access the program.

The class is defined as a friend of another class, it would expose implementation details, thereby reducing encapsulation. Ideally, as much as possible outside hide implementation details of each class.

Examples of data packages

C ++ program, any class with members of the public and private data can be used as examples of encapsulation and data abstraction. Consider the following examples:

#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 the numbers together and returns the sum. Public MemberaddNum and getTotalis external interface, users need to know in order to use their class. Foreign private memberstotal is hidden, users do not need to understand it, but it is a normal class work required.

Design Strategy

Under normal circumstances, we will set up a private class member state (private), unless we really need to be exposed, so as to ensuregood encapsulation.

This is usually applied to the data members, but it is equally applicable to all members, including virtual functions.