search
  • Sign In
  • Sign Up
Password reset successful

Follow the proiects vou are interested in andi aet the latestnews about them taster

C++ data encapsulation

Collection 154
Read 53378
update time 2016-09-11

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.

Hot AI Tools
Undress AI Tool
Undress AI Tool

Undress images for free

AI Clothes Remover
AI Clothes Remover

Online AI tool for removing clothes from photos.

Undresser.AI Undress
Undresser.AI Undress

AI-powered app for creating realistic nude photos

ArtGPT
ArtGPT

AI image generator for creative art from text prompts.

Stock Market GPT
Stock Market GPT

AI powered investment research for smarter decisions

Popular tool
Notepad++7.3.1
Notepad++7.3.1

Easy-to-use and free code editor

SublimeText3 Chinese version
SublimeText3 Chinese version

Chinese version, very easy to use

Zend Studio 13.0.1
Zend Studio 13.0.1

Powerful PHP integrated development environment

Dreamweaver CS6
Dreamweaver CS6

Visual web development tools

SublimeText3 Mac version
SublimeText3 Mac version

God-level code editing software (SublimeText3)