Object-oriented programming in C++ mainly includes the three core concepts of encapsulation, inheritance and polymorphism.
-
Encapsulation :
- Encapsulation is a mechanism in object-oriented programming that bundles data (properties) and functions (methods) that operate on the data into a single unit (class).
- The main purpose of encapsulation is to hide implementation details so that external code cannot directly access the internal representation of the object.
- By using access modifiers such as
private
,protected
andpublic
, we can control the access level of members of a class. - Encapsulation enhances code security and simplifies interfaces. External code uses the public interface to interact with the object without caring about the internal implementation details.
class Box { private: double length; // 私有属性 public: void