C ++ from scratch (12) What is object-oriented programming idea Original Source: Network
The most important concept of C ++-class has been described above, and most of the class-related knowledge has been introduced. So far, we can start to do some advanced programming applications-designing programs, instead of simply turning algorithms into code. To illustrate how to design a program, it is necessary to first understand what programming ideas are.
Programming Ideology
Programming, that is, writing a program. As we have already said, a program is the description of a method, so programming is the description of a method. I knew how to get to the People's Park, and then I wrote a description of the method for getting to the People's Park-from the city center, I walked 400 m east and then turned right and walked 200 m. Then the other person knows how to proceed, but his program is: Walk two intersections along Renmin East Road in the city center, turn right at the third intersection, and go straight to the right hand side. Obviously, the two programs are different, but the last route is the same, that is, the methods are the same, but the descriptions are different.
The so-called programming idea is how to program, that is, how to compile the program. The three steps mentioned in "C ++ from scratch (8)" are actually a programming idea. This is why different people write different programs for the same algorithm (referring to program logic, rather than simple variables or function names). Different people have different programming ideas.
If you compile or read more programs, you will find that programming ideas are very important. Good programming ideas produce well-organized programs with high maintainability. Poor programming ideas produce obscure programs with low maintainability. Note: here we will compare the program's ease of use. In reality, out of efficiency, we will use programming ideas that do not conform to the habits of the human brain, which leads to the difficulty of code maintenance, however, for efficiency, the optimized code is often used at the bottleneck of the Program (this code is generally written in assembly language, and the algorithm is optimized in mathematics to a large extent, discard most of its meaning in the human world ).
This series has always adhered to and recommended such a programming idea-everything is written according to semantics. Semantics is the meaning of language. Previously, it is the meaning of code in the human world. For example, if a table is mapped to a structure with table size, color, and other member variables, why are there no members such as quality, material, price, and production date? It is necessary to explain the meaning of the "Human World.
World
We live in a four-dimensional objective physical world. monsters in the game live in a game-defined world. Snow White lives in a fairy tale world. What is the world? The world is a set of rules. For example, in the objective world, a force can act on an object with mass, and then change the speed of the object according to the laws of kinematics; the charge-specific absorption of the same-sex exclusion; energy conservation, etc, these are descriptions of certain rules in the Rule Set of the objective world. Note that they are only descriptions of rules, not rules, as if the program is a description of methods, but not methods. That is to say, methods and rules are both abstract logical concepts expressed by programs and arguments. Program is what we want to write, and argument is a theory, such as probability theory, kinematics, fluid mechanics, etc. The game world mentioned above is because the game is also a series of rules. I have elaborated on this in another article, game theory, if you have not understood the concept of the world, the elaboration on what is a game in game theory may be helpful. Similarly, the fairy tale world is composed of a series of rules. For example, Snow White can eat, sleep, and be poisoned by Apple viruses. The magic mirror can answer questions.
So what if I understand the concept of the world? What is the purpose? As mentioned above, this series of programs are recommended to be compiled according to semantics, that is, after learning the algorithm, the program is written according to the three steps described in C ++ from scratch (8. Algorithms are based on certain rules. For example, if the algorithm for summation of 1 to 100 is (1 + 100) * 100/2, it implies that some rules already indicate what is addition, subtraction, multiplication, division, what is summation. That is to say, an algorithm must be in one world. It may be meaningless in another world. Because the algorithm is a method, it is operated by the commands mentioned earlier.ResourcesAnd commands and resources are defined by the world.
As mentioned above, writing code based on algorithms is actually a world first developed as a platform for algorithm presentation. For example, if a businessman cross the river, the rule set below shows that one boat can only take two people to cross the river, and three businessmen and three servants are on the river; when the number of servants on any side of the river exceeds that of businessmen, the merchant will be killed. This is a serious and inaccurate description of the world on which the cross-river problem is based, but it is no good to be too abstract. Just note: the businessmen and servants above are not businessmen and servants in the real world, they cannot eat, go to bed, or talk, or even walk. The only thing they can do is to cross the river by boat to change their position. When there are more examples of servants at a location (I .e. a river bank) than merchant instances (and at least one merchant instance), the merchant is killed. The above description is now referred to as the merchant's servant theory. It is a description of the world on which the cross-river problem is based.
The other person does not look at the problem as above. The river has two banks. Each bank has two numbers: merchant and servant. There is one way to change the two numbers corresponding to the river bank according to a rule (that is, the river can only be crossed by two people ), when the number of servants of any river bank is more than that of businessmen (and the number of merchants is not zero), the merchant is killed. This person does not define either the merchant or the servant, but only defines one concept-riverbank, which has two attributes-merchant and servant. This is another argument. It is also called the riverbank theory.
What does it mean? The above are two different arguments about the world where businessmen cross the river. Note that the above arguments are different, but they all describe the same world, just like dynamics and quantum mechanics. They all describe the rules of action between objects in the objective world, but they are quite different. Algorithms are always based on the world, but the more accurate statement is that algorithms are always based on the description of the world. The so-called design program is the world description based on the algorithm, that is, the argument, the argument is actually a description of the problem.
Now, considering the merchant's servant theory and the riverbank theory mentioned above, they both describe the same world, but the former puts forward two concepts of nouns: merchant and servant, each has the "location" status and the "boat ride" function, as well as the verb concept of "Merchant killed". The latter proposes a concept of nouns-riverbank, it has two attributes: "Merchant's number" and "servant's number", and "Merchant's murder" and "boat ride. Here, the latter is better than the former, because the latter defines fewer concepts of nouns (I .e., the concept of nouns is more likely to increase than the concept of VerbArchitectureBecause it represents the variety of things in the world, the more types, the more complex the world, the more difficult to implement), although not necessarily easier to understand, but the structure is simpler.
It is easy to find that all arguments can only be composed of the concepts of nouns and verbs. The former is a number, real number, and plural in mathematics, the latter are addition, subtraction, multiplication, division, and derivation. They are all called definitions. In game theory, I call the former class, And the instance of the class is the resource operated in the method, and the latter is called a command. In the method, the former is the resource type, and the latter is the operation type. The less concepts are proposed, the simpler the structure, and the better. However, it should be noted that, for computer programming, computers are not abstract concepts but efficiency factors, therefore, the code compilation efficiency based on the aforementioned good argument algorithms is not necessarily high.
Therefore, the so-called program design is the arguments based on the design algorithm, and a good program design is a well-designed argument. However, as mentioned above, the efficiency is not necessarily high. In this regard, it is generally designed only at the bottleneck of the code, and the overall architecture of the program is still in accordance with the previous design. As the program grows, a clear and concise program architecture becomes more important. To keep the program architecture concise, you should design a good argument; To keep the architecture clear, code should be written according to the semantics. Next, we will introduce such popular object-oriented programming ideas to help design programs.
What is an object?
To describe object-oriented, first understand what objects are. The object is the implementation of the aforementioned "concept of nouns", that is, an instance. For example, in the merchant's servant theory, there are two "Concepts of nouns": merchant and servant. If there are three businessmen and three servants, there are six objects, there are three merchant instances and three servant instances respectively. Note the differences between objects and instances. In fact, there is no difference between them. If you have to say the difference, you can think that the object can be stateless, but the instance must be stateful.
So what is status? Let's take a look at what is attribute. The table has a property called color. The table is red and the table is green. A person has a state called his face. His face is red and pale. They are all colors, but one is attribute and the other is State. What is the difference? If we map both the table and people into a class, the color of the table and the human face should be mapped into a member variable of the corresponding class, the difference between the two is that the table instance color does not change during the main operation, which is mainly used for reading; the human face may change during the main operation of the Human instance, it is mainly used for writing. What is the operation process? A class maps resources and resources can have functions, that is, member functions. When a function of an instance is executed, it is the operation process of the instance.
There is a function on the table to "put things". When this member function is called, the value of the color attribute is read to determine whether the color of the things placed on the table matches the color of the table. A person has a function of "bath", which can make the face of the corresponding instance change from pale to rosy. But the table also has the function of "changing the color", which can be called to change the color of the table. As mentioned above, the color is an attribute and should be read, but it is written during the operation of the instance. Note that what we mentioned above is the "main operation process", that is, the purpose of the table is to "put things", not to "change the color ". If the concept of a table is mainly used to change its color in the corresponding world, instead of putting something in it, then the table is just a container that records the color value, and the color of the table is the state, it is not an attribute.
What is the significance? Both attributes and states are mapped to member variables, but their meanings are significantly different from those in the code. The attribute is used to configure the instance, And the status is used to represent the instance. In the idea of object-oriented programming, objects are simply examples of attributes and functions (also called methods, this seems very weak when the world on which the program is compiled is complicated, and it is an incorrect understanding of the "attribute", coupled with the word "encapsulation, this leads to a large number of absurd code, which will be explained later.
The difference between attributes and States leads to the so-called stateless object (in MTS --MicrosoftTransaction Server, called Stateless Component, Stateless Component). This is the difference between an object and an instance. An object is an implementation, so it can be an abstract concept. An Instance actually exists, it cannot be an implementation of abstract concepts. In C ++ code, this is represented by a class without member variables and a class with member variables. As follows:
Struct Search {virtual int search (int *, int, int );};
Search a, B; int c [3] = {10, 20, 5}; a. search (c, 3, 20 );
Two objects a and B are generated here, which are abstract