The idea of Object-Oriented Programming Based on C ++ (I)

Source: Internet
Author: User

We all know that,C ++The most important concept is class. After learning about the class, you can start to do some programming-advanced applications-design 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. We recommend that you read this series of articles for your reference.

  • Interpreting Object-Oriented Programming I)
  • Interpreting Object-Oriented Programming II)
  • Interpreting Object-Oriented Programming III)

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. This is also why different people write different program logic for the same algorithm, rather than using different 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.

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 recommends compiling programs based on semantics. 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 algorithms are methods, they are composed of commands and operated resources, and 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 than merchant instances at a certain location, namely a bank of the river, and at least one merchant instance), the merchant is said to have been 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. One way to change the two numbers corresponding to the riverbank according to a rule is to cross the river by taking only two people's boat ), 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 nouns, that is, the latter is more likely to increase the complexity of the architecture than the verb concept, because it represents the types 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 with properties and functions, also known as 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 properties and States leads to the so-called Stateless object proposed in MTS -- Microsoft Transaction Server, called Stateless Component, Stateless Component ), this is exactly the difference between objects and instances-objects are implementations, so they can be the implementation of an abstract concept; instances actually exist, and cannot be the implementation of an abstract concept. In C ++ code, this is represented by a class without member variables and a class with member variables. As follows:

 
 
  1. struct Search { virtual int search( int*, int, int ); };  
  2. Search a, b; int c[3] = { 10, 20, 5 }; a.search( c, 3, 20 ); 

Here, two objects a and B are generated. They are all abstract concepts-objects of the search function. Note that the structure Search does not have a member variable because it is not required. What is the length of a and B? Generally, the compiler sets the length of a and B as one byte, and then & a is not equal to & B.

 
 
  1. struct BSearch : public Search { int search( int*, int, int ); };  
  2. Search *p; BSearch d; p = &a; p = &b; p = &d; p->search( c, 3, 5 ); 

Note that from the code, we can still say that the two Search instances a and B are generated above. Even if a BSearch instance d does not actually exist, the logical size is zero ), that is why there is no difference between it and the object, but there is only a slight difference in concept.

Note that the stateless object mentioned above does not mean the instance of the class without member variables, but does not mean that there is no attribute. For example, the previous BSearch attribute m_MaxSearchTimes indicates that if m_MaxSearchTimes is not found after multiple searches, BSearch: search cannot be found.

Although BSearch has a member variable, it is still an abstract concept logically. Because the implementation of attributes and states is the same through member variables), some special means are required to implement stateless objects. This series is irrelevant and is not listed here.

Is there a difference between a and B? Why do we have two instances? Should the "search function" be mapped to a function based on the previously mentioned semantics? Why do we need to map to a class without member variables? The above usage is used in STLStandard Template Library-standard Template Library), and some variants are made, called function classes. It is a programming technique.

But there is indeed such semantics-the search function has three parameters: the search condition, the search location is the container or set to be searched), and the method of sorting the container or set before searching. Isn't the passing of the function pointer just right? Actually it's not just right. the semantics of the pointer is reference, which is not very accurate here )? This is the so-called object-oriented programming idea.

Next, C ++-based object-oriented programming ideology II)

Related Article

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.