Hello, C + + (31) I finally found the object! 6.1 From structured design to object-oriented programming

Source: Internet
Author: User

Http://www.kuqin.com/shuoit/20150809/347546.html

When C + + falls in love with object-oriented

Many people who enter the C + + world for the first time will ask: What do the two plus signs in C + + mean?

C + + is the development of the language, it is more than the C language of the two Plus, in fact, the C language of the self-increment operator, that the C + + language is the C language on the basis of the addition of new content and evolved. If one of the plus signs means that C + + adds new features to modern programming languages such as templates and exception handling on the basis of C, the other plus adds support for the idea of object-oriented programming based on C + +. It is the new content that these two pluses represent, let C + + on the foundation of C language, complete from tradition to modern, from process to object-oriented evolution, make C + + and C language have the essential difference.

And in the new C + + of these two content, the core is to object-oriented programming thought support, it is it's accession, just completed from C language to C + + gorgeous metamorphosis. So what exactly is object-oriented? Why should C + + add support for it? How does C + + support object-oriented? Don't worry, and listen to my one by one-way.

6.1 From structured design to object-oriented programming

The prototype of object-oriented programming appeared in the Simula language of the 1960. At that time, the field of program design is facing a crisis: in the face of more and more complex hardware and software systems, the traditional C-language-oriented process programming thought has become more and more unable to meet the needs of the reality-the process-oriented design can not well describe the whole system, and the design results are difficult to understand, Thus, the implementation of the software and the maintenance of the late has brought great challenges, the larger the more difficult to achieve, the more difficult to achieve later in the project, people are caught in an unprecedented software crisis. In order to defuse the software crisis, people began to look for the "software crisis" to eliminate the monster "silver Bomb" (bullet). The idea of object-oriented programming is born in this background, and it successfully dissolves the "software crisis" by emphasizing the expansibility and repeatability of design and implementation. Since then, the object-oriented design of the program began in the industry, and gradually become the mainstream. The evolution from C to C + + happens at this time, naturally, C + + also chooses to support object-oriented programming thinking.

The myth of People's month and the silver Bullet

"Human Moon Myth" is a book with far-reaching influence in the field of software. It was born in the context of a software crisis, and it was the book that proposed the concept of "silver Bullets".

In Western myths and legends, monsters can only be killed if they are hit by a silver bullet in the heart. In this book, the authors liken the increasingly large-scale software development projects that are increasingly difficult to manage and maintain to the legendary monsters that cannot be controlled, and hope that a technology can completely solve the software crisis as the Silver Bullet kills monsters.

In fact, the idea of object-oriented programming is not a full-scale silver bullet, it can not solve all the problems encountered by large software projects, but it proposed a more natural way to describe the software, to a certain extent, to solve the software crisis.

6.1.1-oriented process programming for "top-down, gradual refinement"

The simplest and most straightforward way to understand the merits of the new idea is to look at the shortcomings of the old process-oriented thinking. Looking back at the examples we have learned in the previous chapters, we always follow the process of solving problems: Ask questions first, then analyze the process of the problem, and then divide a big problem into small problems according to the process, and if the small problem is still more complex, subdivide it until the small problem can be easily solved. ; Implement each sub-module, solve each small problem; Finally, the main function calls these submodules according to the order of business process, and finally solves the whole big problem, as shown in 6-1. Like this from the problem, from the top down, the progressive refinement of the development of ideas we call "process-oriented design thinking", which describes the main problem-solving "process."

Figure 6-1 Process design-oriented processes

The idea of process-oriented programming was born in the 1960s, and it was the most popular program design idea at that time in the 1980s. Its popularity has its intrinsic reasons, compared with other programming ideas at that time, it has obvious advantages.

1. The program consists of only three basic structures

As described in the 4th chapter of the program flow control structure, the process-oriented programming concept to limit the program only the sequence, selection and circulation of the three basic control structures. Any program logic, whether simple or complex, can be implemented by different combinations or nesting of these three basic control structures. This makes the structure of the program relatively simple, easy to implement and maintain.

2. Divide and Conquer, conquer

When people solve complex problems, they always adopt the strategy of "divide and conquer", and divide the big problem into several small problems, then "conquer" and finally let the big problem be solved. Process-oriented programming thought also take this "divide and conquer" strategy, the larger program according to business logic into multiple sub-modules, and then the division of labor to complete these sub-modules, and finally in accordance with business processes to organize them, and ultimately the whole problem is resolved. According to certain principle, the big problem is subdivided into small problem "conquer", which conforms to the general law of People's thinking problem, its design result is easier to understand, and this method is easier for people to grasp. By decomposing the problem, it reduces the complexity of the problem and makes the program easy to implement and maintain. In addition, the partial decomposition of small problems (sub-modules) can be reused, thus avoiding the duplication of development. But many sub-modules can also be completed by a multi-person division of labor, improve the development efficiency.

3. From top to bottom, gradually refinement

The method of process-oriented programming is "top-down, refinement gradually". The so-called "top down, gradually refinement", is to consider from a macro perspective, according to the function or business logic to divide the program sub-module, define the overall structure of the program, and then the individual sub-modules are gradually refined, and finally decomposed into the program statement so far. This approach allows the programmer to fully consider the problem and make the logic of the program clear. It makes the entire development process from the original thinking "how" to consider "what to do first, what to do", the process is also clearer.

With the development of the Times, software development projects are becoming more and more complex. Although the idea of process-oriented programming has many advantages, its shortcomings are gradually exposed when it solves complex problems: In process-oriented programming, the data and operations are separated from each other, which leads to the change of the structure of the data, and the corresponding operation function has to be rewritten again. , if there is a change in demand or new requirements, it may involve the re-partitioning of the module, which will modify a large number of previously written functional modules. The separation of data and operation in process programming is characterized by the combination of some modules with the specific application environment, and the old program modules are difficult to be reused in the new program. The inherent shortcomings of these process-oriented programming ideas make it more and more unable to adapt to the development of large-scale software projects, which is "process-oriented, losing and process-oriented". So, people began to look for a new idea of program design. It is in this case that some new ideas of program design begin to emerge and gradually replace the thought of process-oriented programming, and the object-oriented programming idea is one of the "leading brothers".

6.1.20,000 objects: Object-oriented Programming

Object-oriented programming (oriented programming, OOP) is the inheritance and development of process-oriented programming, which not only draws the essence of the latter, but also analyzes and solves the problem in a way closer to human thinking: The procedure is the abstraction and description of the real world, The basic unit of the real world is the object, which corresponds to the basic unit of the program.

The object-oriented thought is that the real world is composed of many entities that are related to each other and exchange information. As large as a planet, a country, small to a person, a molecule, whether it is a life, or no life, can be regarded as an object. By analyzing these objects, each object is found to consist of two parts: data (variables) that describe the state or properties of an object, and methods (functions) that describe the behavior or functionality of the object. In contrast to the function of process-oriented data and manipulation of data, object-oriented objects combine the functions of data and manipulating data together to form objects to more accurately describe the real world. This can be said to be the most essential difference between the process-oriented and object-oriented.

Corresponding to the real world, in object-oriented, we use an object to represent an entity in the real world, each object has its own properties and behavior, and the whole program is composed of a series of interacting objects, the objects through each other to complete complex business logic. For example, in a class, there is a teacher Chen and 50 students, then we can use a teacher object and 50 student object to abstract and describe this class. For these 51 objects, some properties are common to them, such as name, age, and so on, and some attributes are specific to a particular class of objects, such as the teacher object has the attribute of the job, and the student object does not. In addition, teachers and students of the two kinds of objects have their own behavior, such as teachers have to prepare for lessons, classes, correcting homework behavior, while the student object is to listen to, complete homework and other behaviors. Teachers and students are responsible for their own behavior and responsibilities, but also related to each other, such as the teacher's action in class needs to take students as the object of action. Through the interaction between the objects, the whole class can function normally. The results of the entire object-oriented analysis design are very close to our real world and are naturally easier to understand and implement. The teacher is shown in Object 6-2.

Figure 6-2 using object-oriented thinking to abstract teachers into objects

Know more: What is the importance of object-oriented programming?

This, perhaps, can be said from the birth of the object-oriented.

Before object-oriented birth, there was a process-oriented person who abstracted the whole problem to be solved as a description of the data of the thing and a function describing the processing of the data, or the process of data processing. When the scale of the problem is small and the demand changes little, the process is working very well.

But (anything is afraid of "but" two words), when the scale of the problem is getting more and more complex, the demand changes more and more quickly, the process seems to be inadequate. Imagine that when we modify a structure according to the requirements change, we have to modify all the process functions associated with it, and a process function modification, often involves other data structures, when the system is small, this is relatively easy to solve, but when the system size is getting bigger, Especially when it comes to multi-person collaborative development, it's a nightmare. This is the famous software crisis.

In order to solve this software crisis, object-oriented came into being (there is a problem, there must be a solution to the problem of the emergence of the hero, most of the birth of this).

We know that object-oriented kick are encapsulation, inheritance, and polymorphism: It uses encapsulation to combine the data in the problem with the functions that process the data, forming the concept of a whole class, which is more in line with people's thinking habits, more conducive to understanding, and it is easier to understand and abstract some complex systems naturally. It uses inheritance to deal with the expansion of the system, on the basis of the original system, as long as the simple inheritance, you can complete the expansion of the system, without reinvent; it uses polymorphism to respond to changes in demand, unified excuses, but can have different implementations.

It can be said that the object-oriented thought uses its kick to solve the software crisis to some extent, which is the fundamental embodiment of its importance.

Hello, C + + (31) I finally found the object! 6.1 From structured design to object-oriented programming

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.