[C++] Class encapsulation ① (Classes and objects | Three major characteristics of object-oriented - encapsulation, inheritance and polymorphism | Introduction of class encapsulation)





1. Classes and objects




1. Class and object concepts


"Object-oriented programming" is a "programming paradigm" that can be applied to all high-level languages, including C++;

Object-oriented programming is based on the concept of "object", in which data (member variables) and operations (member methods) can be encapsulated;

Object-oriented programming provides better abstraction and modularization of external things, making the code easier to understand;


The following introduces two important concepts of object-oriented classes and objects;

  • "Class" describes the data (member variables) and operations (member functions) of the object, and is the blueprint for defining the object;
  • "Object" is an instance created from a class and is a specific representation of the class. Each object has its own member variables and member functions;

2. Code examples - defining classes and objects


Define the class: Create the MyClass class, which defines the myVariable member variable and myFunction member method;

class MyClass {
    
      
public:  
    int myVariable;  // 成员变量  
  
    void myFunction() {
    
      // 成员函数  
        // 函数体  
    }  
};

Define the object of the above class: define the instance object myObject of the above MyClass class;

MyClass myObject;  // 创建一个 MyClass 类型的对象




2. Class encapsulation




1. Three major characteristics of object-oriented


Three major characteristics of object-oriented:

  • Encapsulation: Encapsulating data and operations in a class can hide the implementation details inside the class and only expose a limited interface to interact with the outside, thereby protecting the internal state of the class object from being arbitrarily modified by the outside;
  • Inheritance: Create a new subclass and inherit the properties and methods of the existing parent class. The subclass can add new properties and methods to achieve more powerful functions, and can also override methods in the parent class to achieve different behaviors. ; Through inheritance, a hierarchical class hierarchy can be built, thereby promoting code reuse and extension;
  • Polymorphism: An interface or parent class reference can point to multiple actual types;

The three major characteristics of object-oriented encapsulation / inheritance / polymorphism together constitute the basic principles of object-oriented programming, providing support for code readability / maintainability / scalability;


2. Introduction of class encapsulation


Encapsulation: Encapsulating data and operations in a class can hide the implementation details inside the class and only expose a limited interface to interact with the outside, thereby protecting the internal state of the class object from being arbitrarily modified by the outside;


Encapsulate objective things that exist in the real world into abstract classes, which contain data and operations.

  • Only trusted classes or objects can access hidden information;
  • By default, most classes can only access the interfaces exposed by the class;

In a C++ class, the access level of class member variables and member methods is defined through "access control modifiers" ;

Guess you like

Origin blog.csdn.net/han1202012/article/details/132733023