Technology Sharing

[C] Encapsulation

2024-07-12

한어Русский языкEnglishFrançaisIndonesianSanskrit日本語DeutschPortuguêsΕλληνικάespañolItalianoSuomalainenLatina

1. Definition of Encapsulation

Encapsulation is one of the three basic characteristics of object-oriented programming (OOP) (encapsulation, inheritance, polymorphism). It refers to combining data (attributes) and methods (functions) that operate on these data into an independent unit (class), hiding the internal details of the object as much as possible, and only exposing the interface to the outside world. The purpose of this is to protect the object's data, prevent external code from directly accessing the object's internal data structure, reduce errors and simplify complexity.

2. Purpose of Encapsulation

  1. Data hiding:Protect the data of the object, avoid direct external access, and prevent the data from being illegally modified.
  2. Clear interface: By exposing limited interfaces (methods), external code can only interact with the object through these interfaces, thereby clarifying the behavior of the object.
  3. Modularity: Encapsulate related data and operations together to improve the modularity and reusability of the code.

3. Methods for implementing encapsulation in C++

  1. Using Class: When defining a class, set the data members (attributes) to private or protected, and set the methods that operate on these data to public. In this way, external code cannot directly access the private members of the class, but can only access them indirectly through public methods.

  2. Access Modifiers

    • public: Any external code can access.
    • protected: Can only be accessed in the class itself and derived classes (subclasses).
    • private: Can only be accessed in the class itself, not in derived classes.
  3. Constructors and Destructors: Initialize the object through the constructor and clean up the resources occupied by the object through the destructor. They are also part of encapsulation because they control the life cycle of the object.

Example

The following is a simple C++ wrapper example showing aRectangleImplementation of the (Rectangle) class.

  1. #include <iostream>
  2. class Rectangle {
  3. private:
  4. double width; // 矩形的宽
  5. double height; // 矩形的高
  6. public:
  7. // 构造函数
  8. Rectangle(double w, double h) : width(w), height(h) {}
  9. // 设置宽度
  10. void setWidth(double w) {
  11. width = w;
  12. }
  13. // 获取宽度
  14. double getWidth() const {
  15. return width;
  16. }
  17. // 设置高度
  18. void setHeight(double h) {
  19. height = h;
  20. }
  21. // 获取高度
  22. double getHeight() const {
  23. return height;
  24. }
  25. // 计算面积
  26. double getArea() const {
  27. return width * height;
  28. }
  29. };
  30. int main() {
  31. Rectangle rect(5.0, 10.0);
  32. std::cout << "Rectangle area: " << rect.getArea() << std::endl;
  33. rect.setWidth(7.0);
  34. std::cout << "New Rectangle area: " << rect.getArea() << std::endl;
  35. return 0;
  36. }

In this example,RectangleClasswidthandheightProperties are made private so that external code cannot access them directly. Instead, we access them through public methods such assetWidthgetHeightandgetArea) to access and modify these private properties. This approach implements data encapsulation and provides a clear interface for external use.