C ++ starts from zero (12) - What is object-oriented programming ideas?

xiaoxiao2021-03-06  106

C from scratch (12) - What is the most important concept of C in front of object-oriented programming, and introduces most of and class-related knowledge, and it is already possible to start doing some programming. Advanced Applications - Design Programs, no longer just turn the algorithm into code. To explain how to design the program, it is necessary to understand what to program our mind. Programming ideas, that is, write programs, and before, the program is a description of the method, then programming is a description of the writing method. I know how to go to the People's Park, and then I have written a description of the way people's park - starting from the city center to 400 meters to the east and turn to the right 200 meters. Then another person knows how to go, but his procedures are - from the city center walking through two intersections along the people's East Road, turn right at the third intersection, can go straight to the right hand . Obviously, the two programs are different, but the last route is the same, that is, everyone's method is the same, but the description is different. The so-called programming idea is how to program, that is, the method of writing a program. The three steps that have been programmed in "C from zero (eight)" are actually a programming idea. This is also why the different people have different procedures written by the same algorithm (referring to the logic, not simple variables or function names), and different people's programming ideas. If you don't look at some programs, you will find that programming ideas is very important. Good programming idea, the program compiled, highly maintainable; poor programming ideas, is difficult to understand, low maintenance. Note that this is the easy-to-readability of the program. It actually uses the efficiency, which uses the programming idea that does not conform to the human brain thinking, which causing the code to be difficult to maintain, but for efficiency, it will often be in the bottleneck of the program. Using an optimized code (this code is generally written in assembly language, the algorithm is largely mathematically optimized, and most of its significance in the human world) is discarded. This series has always insisted and recommending such a programming idea - everything is written in semantics. Semantics is the meaning of language, before it is the meaning of code in the human world. For example, a table, mapped into a structure, a member variable such as a table, color, so why don't you have a quality, material, price, production date, etc. It is necessary to explain the meaning of "human world". World we live in a four-dimensional objective physical world, the monster in the game lives in the game defined game world, and Snow White is living in a fairy tale world. What is the world? The world is the collection of rules. For example, in the objective world, the force can act on the quality of objects, and then change the speed of the object in accordance with the law of the law; the charge isotropic phase suction is similarity; energy conservation, etc., these are the collection of this rule of the objective world. Description of some rules. Note that they are just a description of the rules, not rules, as if the program is a description of the method, but not a method. That is, the methods and rules are abstract logical concepts, each by program and argument. The program is what we have to write, but the argument is a theory, such as probability, sports, fluid mechanics, etc. The game world mentioned earlier is because the game is also a series of rules, about this, I have done a detailed explanation in another article "Games", if I haven't understood the concept of the world, "game" On what the game is intended to help. Similarly, the fairy tale world is also composed of a series of rules. For example, Snow White can eat, you can sleep, and can poison it because of the poisonous apple; the magic mirror can answer questions.

So, even if you understand this concept of the world? What is the use? As mentioned earlier, this series is recommended to write a program in semantics, that is, after the algorithm is written in accordance with the three steps that C from zero (8) ". The algorithm is based on certain rules. If the algorithm given 1 to 100 summation is (1 100) * 100/2, this is implicit that there are already some rules that explain what is addition and subtraction, what is the sum. That is, an algorithm must be a world, it may be meaningless in another world. Because the algorithm is the method, it is made up by the previous command and the resource being operated, and commands and resources are defined by the world. The foregoing said that according to the algorithm, it is actually a platform for the algorithm to display according to the algorithm. If the previous businessman passes the river, it is in the following rules set: There is a boat that can take the two people can manage the river; there are three businessmen and three servants on the side of the river; the servant of the river Many business people will be killed. This is a serious inaccurate description of the world based on the river problem, but there is nothing benefits in this abstraction, just pay attention to: The above businessman and servant are not the businessman and servant in the real world, they can't eat, can't sleep Speaking, even walking will not work, the only thing is to change its own position by sitting on the river. When a certain location (ie, a corner of the river) is more than the instance of the businessman (and there is at least one of the business people), it is called the businessman to be killed. The above description is called a businessman's servant, which is a description of the world based on the river problem. Another person is not as good as above. There are two shores of the river, and each river bank always corresponds to two numbers - the number of business people and the number of servants. There is a way to change the two numbers corresponding to the river bank according to a rule (that is, sitting can only take the two people), and when any of the number of servants corresponding to the river bank is more than the number of business people (and the number of business people is not Zero), the businessman is known. This person does not define two concepts of businessmen and servants, but only defines a concept - river bank, this concept has two properties - the number of business people and the number of servants. This is another argument, which is called the riverore theory. What do you mean? The above is two different arguments for the world based on the problem of the river. Note that the above argument is different, but the description is the same world, as if kinetic and quantum mechanics are descriptions of the role of objective world objects, but large phase mild. The algorithm is always based on one world, but more accurate statement should be based on the algorithm, based on a world description, and the so-called design procedure is the world description, ie, the argument, the argument, and the argument, is actually a description of the problem. Now consider the service of the servant and riverside theory, they are all descriptions of the same world, but the former presents two morphic concepts - businessmen and servants, each with "position" and "sitting boat" such a function and "Businessman is killed" this vernic concept; the latter proposes a name concept - river bank, with "business people" and "number of servants" and "merchants are killed" and "sitting boat" two verbic concepts . Here, the latter is better than the former, because the latter definitions are less known, which is more likely to increase the complexity of the architecture than the morphological concept, because it represents the type of the world, the more types The more complex the world, the more difficult to achieve), although it is not necessarily to understand, but the structure is simpler. It is easy to find that all the arguments can be composed only by "Nouvetical Concept" and "Verir Concept", where the former is the number, real, plural, etc. in mathematics, the latter is the addition and subtraction, the guide, etc., they are all Called the definition. In "Game Theory", I called the former as class, and the instance of the class is the resources operated in the method, the latter called command.

In the method, the former is the type of resource, the latter is the type of operation. An argument, the less the concept, the more simple structure, the better. However, it should be noted that the computer programming is not necessarily high because the computer is not an abstract concept, but is the efficiency factor. Therefore, the so-called programming is the argument based on the design algorithm, and a good program design is good for the corresponding argument. However, the above is said, the efficiency is not necessarily high, in this, generally only in the bottleneck position of the code, and the overall architecture of the program is still in accordance with the previous design. As the program is increasing, the clear and concise program architecture is more important, but to maintain the conciseness of the program architecture, it should be designed; to maintain the clearness of the architecture, it should be written according to semantics. Below, this paper introduces such a popular object-oriented programming idea to help the design program. What is the object to explain the object-oriented, first, what is the object. Object is the implementation of the "Nounny Concept" mentioned above, that is, an example. For example, there are two "native concepts" in the servant of the merchant, and there are three businessmen and three servants, and there are six objects, which are examples of three businessmen and the examples of the three servants. The differences of objects and instances should be considered different. If they do not say differences, it can be considered that the object can be in state, but the instance must be status. So what is the state? Still let's take a look at what is attribute. The table has a property called color, this table is red and that is green. The person has a state called a face, and the face of this person is ruddy and the pale. They are all colors, but one is the property is a state, what is the difference? If you map the table and people into classes, then the color of the table should be mapped into a corresponding class member variable, and the difference between the two is that the color does not change in the main operation process, which is mainly used The human face may change during the main operation of the human instance, which is mainly used to write. What is the operation process? Class maps are resources, resources can have functions, that is, member functions, when an instance is executed, is the operation process of this instance. The table has a function of "put things", when calling this member function, where the value of the color this property is read to determine whether the color of the thing on the table is coordinated with the color of the table. A function is "bath" that allows the face of the corresponding instance from the horrible to the ruddy transition. But the table also has a function to "change the color", call it to change the color of the table. As mentioned earlier, the color is attribute and should be read, but it is written in the operation of the instance. Note that the "main operation process", that is, the purpose of the table is to "put things", not "change color". If this concept is mainly used in its corresponding world, it is used to change its color rather than put things. At this time, the table is just a container that can record color values, and the color of the table is status, not attribute. What is the meaning? Both attributes and status are mapped to member variables, which can not see their differences from the code, but their semantics are serious. The property is used to configure instances and status is used to express instances. In object-oriented programming, it is simply that the object is an instance with attributes and functions (also known as methods), which is very weak when the world written is complicated, and it is "attribute". Error understanding, coupled with "package" vocabulary swept, resulting in a lot of ridiculous code, will be described later.

The difference in attributes and status results in a so-called stateless object (in MTS-Microsoft Transaction Server, called Stateless Component, stateless components), which is the difference between objects and instances - objects are implemented, so it can be Implementation of an abstract concept; example is actual existence, not an abstract concept. This behaves in C code as classes without member variables and 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); here Generate two objects A and B, they are all abstract concepts - the object of the search function. Note that the structure Search does not have a member variable, because it is not required, how much is the length of A and B? Since the following conditions may occur, the general compiler will set the length of the above A and B as one byte, and then & A is not equal to & b. Struct Bsearch: public search {int search (int *, int, int);}; search * p; bsearch d; p = & a; p = & b; p = & d; p-> search (C, 3, 5); Note It is still known from the code to generate two instances A and B, BSearch's instance D (even if they do not exist at all, logically), this is why it is said to be, there is no Difference, only conceptual minor differences. Note that the state mentioned in the previously mentioned state is not a case of a class without member variables, but there is no state, does not mean no attributes. As the Bsearch may have a property M_MaxSearchTimes to indicate a half search, if Search M_MaxSearchTimes is still not found many times, then Bsearch :: Search returned to find. Although BSearch has a member variable, it is logically it is still an abstract concept. Due to the realization of attributes and status (all pass the member variable), there is a special means of achieving stateless objects. Since it is not related to this series, it is not a table. Does the front A and B have differently? Why have two instances? "Search Function" is not more mapping as a function as before. Why is you mapped into a class without member variables? The above usage is used in STL (Standard Template Library-Standard Template), made some modifications, called function classes, is a programming skill. However, this semantic-finding function has three parameters: lookup conditions, lookup location (i.e., the container or collection of you want to search), find the method of prenating the container or collections. Is the function pointer not just right (actually not just right, the semantics of the pointer are referenced, is this not very accurate)? This is the so-called object-oriented programming idea. Object-Oriented Programming Thoughts The design procedure is the description of the problem of writing a problem solving, that is, writing arguments. The argument can be manifested by "Nounny Concept" and "Veridistic Concept", and the object is just the implementation of "Nounny Concept", and the class that does not have member variables that do not have member variables can be mapped "Verbic Concept" Convert it to an object.

Therefore, one world can completely consist of objects, and the world based on the algorithm only exhibits only objects, and then performs subsequent code, this programming method is called the object-oriented programming idea. Note that the context should be based on the world, and then all objects will be expressed, and then set up the calculation method, and finally mapped to code. But when writing a businessman's crossing, it is directly given the algorithm, and does not design the world? In fact, because the problem is too simple, I directly designed the world, and describe it with the river bank discipline mentioned earlier. Attention should pay attention to the problem of the problem, but accurately, I didn't design the world in front, but designed the riverside theory to describe the problem. Then, since the object is an example, the world is described in C in C , and the performance world is combined by an instance of the class. However, it should be noted that object-oriented is to describe the world with an object, but also describe the algorithm, because the algorithm will also propose some concepts that need to be mapped, such as the river program in the algorithm of the front businessman crossing the river. But remember that when describing the algorithm, the class defined when describing the world, must maintain the design of the class, do not map the part of the algorithm to this class as a member of the algorithm for the operation of the algorithm The function, because this seriously obscures the implementation of the algorithm, destroying the architecture of the program. If an algorithm is to keep the car in place, it needs a complex operation. Is it possible to add a function to the car, let it keep it in place? ! This is often made in the design class, and for this reason, an object-oriented code is not imagined only by the class, which may also have a large number of maps to certain operations in the algorithm. Global function. Keep in mind: If you are a class, if you are mapped to the concept of the world, don't consider the algorithm, just design it in this world, don't give it a wrong member because of a certain need in the algorithm. Therefore, the "Nouncing Concept" maps into classes, "Nounny Concept" properties and status maps to member variables, "Nouvet Concept" features a functional mapping as a member function. So what should I do if "verb concept"? Map into a class with no member variables? As far as seen, due to too much awkward, this practice is not common (STL is just a skill), so it is often mapped into functions, although this is back to object-oriented thinking, but Easy to understand, and then the architecture of the program makes sense. With the advent of object-oriented programming, a new design is born. Since it is so good, it is widely circulated, but understands the mistakes caused the wrong ideology, even worse is the reverse, and this design is called the object-oriented programming idea, its name is package. Package first look at the design of the following categories that are often visible in the explanations of the object in various VC tutorials.

class Person {private: char m_Name [20]; unsigned long m_Age; bool m_Sex; public: const char * GetName () const; void SetName (const char *); unsigned long GetAge () const; void SetAge (unsigned long); Bool getsex () const; void setsex;}; top all the members variables are all defined as private, then provide three pairs of Get / Set functions to access three member variables above (because they are private, the outside world cannot be directly Access), these three pairs of functions are public, why is this? Those textbooks referred to here as package, is packaged in the package of Person's internal memory layout, so that the outside world does not know how it is layout in memory and in turn to ensure the effectiveness of memory (acting only by the class itself). The first thing to confirm the absurdity of the above design, it is authentic "there is no lock" is meaningless. Then look at the so-called package of memory layout. Recalling why each of the start files of the source file to use the class will contain the appropriate header files in "C from scratch (ten)". Suppose is the declaration in Person.h, then use the class Person in B.CPP, I have to be included in #include "person.h", now replace it: Class Person {public: char m_name [20]; unsigned long M_age; bool m_sex; public: const char * getname () const; void setname; unsigned long getage () const; void setage (unsigned long); Bool getsex () const; void setsex (bool); The class Person is then used in B.CPP, as follows: Person A, B; A.M_age = 20; B.GetSex (); here, use Person :: m_age, even if you don't do this, you don't do this. Still #include "person.h", as follows: struct person {char m_name [20]; unsigned long m_age; bool m_sex;}; person a, b; person * pp = (person *) & a; pp-> m_age = 40 The above still modifies the members of Person's instance A, how can I hide the memory layout? ! Recall the role of the statement, the memory layout of the class is necessary when the compiler generates an object, and you can't hide anything about the object's implementation at all, otherwise the compiler cannot compile the corresponding code. Then look from semantics. Person map is not the concept of people in the real world, should be a buffer in the table in the table of a recorder information in a database, then the buffer should have the function represented by the three pairs of GET / SET? ? The buffer is used by buffer data. After the buffer is used by other operations, it is like a box, just putting the way.

Therefore, the top three pairs of GET / SET do not exist, and three member variables cannot be private. Of course, if the Person map is not a buffer, but in other worlds, there is no problem like the semantics like above, it is no problem like it is, but if it is because of the encapsulation of the memory layout, it is big. Error. The above error is worth not understanding. To illustrate the package, look at the MFC (Microsoft Foundation Class Library - Microsoft Function ", a library file defining many classes, which is packaged. About library files in Illustrating SDK) Definition of class CFILE. From the name, it can be seen that it maps the concept of files in the operating system, but it has such a member function - CFile :: Open, CFile :: Close, cfile :: read, cfile :: write, what is the problem ? The four member functions maps are functions for the operation of the file instead of the file, which opens the file, closes the file, writes data from the file, and write data to the file. Is this not a semantic opposite of the member function? The above four operations have a commonality, which is applied to the resource of the file, which can be called "function", such as the file with "opened", with "read" function, but should pay attention to They are not actually the function of the file. According to the original statement, the file should be mapped into a structure, such as File, then the above four operations should be mapped into four functions, then use the functionality of the namespace, as follows: Namespace ofile {BOOL Open (file &, ...); bool Close (File &, ...); Bool Read (File &, ...); Bool Write (File &, ...);} The above name Space Ofile indicates that the four functions in the file are the operation of the file, but the four functions have one File & parameters. Recalling that the non-static member function has a hidden parameter this, so a great idea is born. Describe all the sets of operations for some resource as a resource, map it into a class, the object of this class is the operation of an object, this method is called a package, and that class is called Packaging class or package class. Obviously, the packaging class maps "Operation to a resource" is an abstract concept, that is, the object of the packaging class is a stateless object (referring to the logically stateless object, but if multiple operations are connected It may still be status, but at this time it varies accordingly. Such as one CFile :: FLUSH member function, used to refresh the buffer content, then there is at least one state-buffer, It can also have a status record that has been called CFILE :: Write, no refresh is not needed). It is now possible to understand the meaning of the package. The operation of some resource is encapsulated into a class. This packaging class map is not a "named" concept "defined in the world, but the world's" words "or algorithm" is a concept of a concept " This person is abstract concept. Since the packaging class is a package of the operation of a certain resource, the package object must have a property indicating that the object being operated. For the CFile in the MFC, it is the cfile :: m_hfile member variable (type Handle), which is in the package class The main operation process of the object is read in the CFILE :: Read and CFile :: Write.

what is the benefit? The package provides a means to convert some "verbic concepts" in the world into objects, making the architecture of the program easier (multiple "verbic concepts" becomes a "noun concept", reducing "Verbism Concept) "The quantity), more tended to object-oriented programming ideas. However, you should distinguish between the package object and the packaged object. The packaging object is just a housing, and the object being packaged must be a status of a state because the operation is a state of changing the resource. For CFILE, CFILE examples are packaged objects that maintain a reference to the package object-file kernel object (a resource defined in the Windows operating system, the example characterization of Handle) - in CFILE: : m_hfile. Therefore, the packaging object is independent of the packaged object. That is, CFILE A; At this time, the value of A.m_HFile is 0 or -1, indicating that the object thereof is invalid, so if A.Read (...); will fail because the resource applied is invalid. In this regard, a.Open (...) should be called first; to bind A and a specific file kernel object, call A.Close (...); will be released. Note CFILE :: Close is only released, and it does not mean that A has been destroyed because A map is not a file kernel object, but a packaging class object operated on the file kernel object. If you think about it, you will find that the tiger can eat rabbits, the rabbit can be eaten, should it be that the tiger has a function of "eat rabbit" or a plurality of rabbit packaging classes to encapsulate the "eating rabbit"? This is actually any problem, "Tiger eats rabbit" and "rabbit are eaten" is completely two different operations involving two resources, and the latter only involves a resource, so it can be achieved at the same time, specific application The semantics of their respective worlds. If you have a "eating" function, you can eat "meat" and "autonomic" multiple inheritance from "meat" and "autonomic" inheritance, rabbit. Here is a "autonomous ability", which means animals have awareness and can move itself. This is a class with a member function in C , indicating that the function can be operated, but the radio also has a staging and other functions. Can the radio also move? ! This is the meaning of the world - running. Method - The world's drive mode algorithm is a method, which has been described above, which has been described by the operation and the type of resource, ie the type of resources and operations. Method points to how to use the various operations defined in the world, but do not execute. From the foregoing explanation, the world can consist only by the object, after the object is generated, the state and attribute of all objects in the world, that is, a member variable, a copy, a snapshot called the world, and the state of the world Change is called the world's operation. The state of the world is the status and attribute of all objects in the world. To change it, it is to perform the world-defined operation, but can only point to how to perform it to change the world, and drive the world, even if the world is defined Execution can drive the world. The more far the above, the farther, I feel that it is very meaningful? Consider why the concept of the world is going to make. The world is the rule set body based on the problem we want to program, and the design procedure is the design description of the world's argument, and then the design algorithm is designed, written out code, execute code, and get results. "got the answer"? ! What is the result? That is, a part of the world's final state, such as the value of the circumference. This is actually purposeful, but it is worth noting this.

The process of code execution is often another purpose, such as saving data into a file; open the editing and saving, this purpose is not concerned about the state of the world. The current state of the world is the world's operation is another very popular purpose - video game. No matter what kind of purpose, it is necessary to change the state of the world, that is, to drive the world operation, it must be implemented by the world's definition, and this can only be implemented by way. Therefore, in the design algorithm, the way to drive the world is also determined. For the first purpose above, since it is to see the final state of the world, it is continuously executed to the end. Examples of the businessman who gave the river from the C from zero (8) "are printed and ending after obtaining the result through the algorithm. For the second purpose, since the execution process is performed, the operation can be continuously executed. However, this purpose often requires the user to decide when to perform and execute more code, after the file is opened until the user gives an editing operation, and the user may be randomly implemented. Different editing operations, and finally determine whether the file is saved by the user. This world is completely called a user-driven way for the world drive mode controlled by the user. The algorithm here is just how to open, save the file, how to edit data, but because the decision is a user-driven mode, the algorithm must be modified to implement this driving method. Look at the third purpose, it is a change in the state of the world, and there are two previous types. However, it is obvious that the first change process cannot be continuous, and it will be completed continuously; the second type is driven by the user, it is too much trouble. Therefore, there will often be a loop, which is generally called the main loop in the game program. Each cycle changes the state of partial objects in the world in accordance with certain rules, which is called a cycle drive mode. Each cycle will be changed to an example of an animal, such as an animal mentioned earlier. In addition, it is not called an example of autonomic initiative, such as the radio mentioned earlier. Similarly, the algorithm in the game still does not involve the loop driving method mentioned above, so the algorithm must be modified to achieve a loop driving method. The above will not explain it for a little, and it is no longer possible to write a program as "C from scratch (eight)". A method is given below. 1. When a problem is obtained, the algorithm for this problem should be obtained (the programmer is not a scientist), or given by the customer or derived directly from too simple. 2. Designed by the problem, it is designed, that is, the above-mentioned argument is the so-called programming. 3. Describe the previous algorithm for the previously designed argument, and improve this argument (because the algorithm may be brought into some concepts that do not exist in the world). 4. What kind of world-driven way you need to use, and implement the algorithm and argument (the world's drive mode may also be brought into some concepts that do not exist). 5. Continue "C from scratch from scratch (eight)". The steps given here are only general, and when the world is too complicated, the second step will be subdivided. First design the big frame; again design the interface; finally determine the specific application of the interface. About the interface will explain in "C from zero (18)", and the design of the interface mentioned herein does not need to be used, it simply makes the design, not necessary. Because it is not related to this series, it is absent.

转载请注明原文地址:https://www.9cbs.com/read-126970.html

New Post(0)