imx6ull QT programming under Linux system - C++ data encapsulation and data abstraction (8)

Linux QT programming


foreword

Encapsulation is a concept in object-oriented programming that binds data and functions that manipulate data together, so that it can avoid interference and misuse from the outside world, thereby ensuring security. Data encapsulation leads to another important OOP concept, data hiding. Data encapsulation is a mechanism that binds data and functions that manipulate data together. Data abstraction is a mechanism that only exposes interfaces to users while hiding specific implementation details. C++ supports encapsulation and data hiding by creating classes ( public, protected, private).

In a C++ program, any class with public and private members can be used as an instance of data encapsulation and data abstraction. Under normal circumstances, we will set the class member state to private (private), unless we really need to expose it, so as to ensure good encapsulation. This generally applies to data members, but it applies equally to all members, including virtual functions.

1. Data encapsulation

Taking dogs as an example, add a food method addFood(int number). Set the method of obtaining food under public, so that the addFood(int number) method is exposed, which is the external interface. Then we set the total number of servings of the dog's private member (private) food.

Then we also write a public method getFood() under public, and use getFood() to print out how many meals the puppy has obtained in total.
Create a new directory 10_encapsulation_example, edit a 10_encapsulation_example.cpp with the following content.

1 #include <iostream>
2 #include <string>
3 using namespace std;
4
5 class Dog
6 {
    
    
7 public:
8 string name;
9 
10 Dog(int i = 0)
11 {
    
    
12 total = i;
13 }
14 
15 void addFood(int number) {
    
    
16 total = total + number;
17 }
18 
19 int getFood() {
    
    
20 return total;
21 }
22 private:
23 int total;
24 };
25
26
27 int main()
28 {
    
    
29 Dog dog;
30 
31 dog.name = "旺财";
32 
33 dog.addFood(3);
34 dog.addFood(2);
35 
36 cout<<dog.name<<"总共获得了"<<dog.getFood()<<"份食物"<<endl;
37 
38 return 0;
39 }

Lines 10 to 13, initialize the total number in the constructor, and the default value of the total number without initialization is the number of int type. So we need to initialize in the constructor, which also reflects the function of the constructor, usually initialized in the constructor. Do not assign and initialize directly within the class.
Lines 15 to 17, addFood(int number), in this method, assign the obtained number of food servings to total.
Lines 19 to 21, getFood(), in this method, will return the total number of servings of food. By calling this method, you can access the total number of private members.
Lines 33 and 34 add the number of servings.
On line 36, print the total number of servings of the food.

Then we compile and run
insert image description here

2. Data abstraction

Data abstraction means that only key information is provided to the outside world, and the implementation details of its background are hidden, that is, only necessary information is displayed without presenting details. Data abstraction is a programming (design) technique that relies on the separation of interface and implementation.
Benefits of data abstraction:

  1. The internals of the class are protected from corrupting the state of the object due to inadvertent user-level errors.
  2. Class implementations may change over time in response to changing requirements, or in response to bug reports that require no changes to user-level code.
    To give a simple example, such as mobile phones in our lives. Mobile phones can take pictures, listen to music, radio and so on. These are functions on the mobile phone that users can use directly. But how the function of taking pictures is realized, how to capture images through the camera and then display them on the screen, as a user, you don't need to know. That is to say, the exposure does not need to be too thorough, and users do not have to know how this function is realized, they only need to know how to take pictures. As far as C++ programming is concerned, C++ classes provide the possibility for data abstraction. They provide a large number
    of public methods for manipulating object data to the outside world, that is to say, the outside world does not actually know the internal implementation of the class. In fact, the object like cout is a public interface, we don't need to know how cout displays content on the screen. cout is already implemented under the hood.

おすすめ

転載: blog.csdn.net/qq_51963216/article/details/130973037